Questions tagged [throws]

throws is a Java keyword. It is used in a method definition to declare the Exceptions to be thrown by the method.

Java distinguishes between checked and unchecked exceptions. Unchecked exceptions (RuntimeException, Error, and their subclasses) can be thrown without restriction. But if a method can generate a checked exception, the compiler requires that it either be caught (with a try/catch block) or declared in the method signature with the throws keyword.

https://en.wikibooks.org/wiki/Java_Programming/Keywords/throws https://docs.oracle.com/javase/tutorial/essential/exceptions/declaring.html

249 questions
1
vote
1 answer

Checking all locations in an array before returning a variable

I have this method that returns a Recipe object if found by name in my Menu array. If it doesnt find it, it throws a custom RecipeNotFoundException and just says it isnt found... public static Recipe getRecipe(String recipeName){ try{ …
Sherifftwinkie
  • 391
  • 1
  • 13
  • 21
1
vote
1 answer

How to suppress error for missing `throws`

Is there a way to get Eclipse to suppress errors for a missing throws declaration when throwing exceptions within methods? If you are to build and run something without explicitly specifying throws, it will unwind to the main method and crash the…
Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
0
votes
3 answers

Catching Exceptions From Multiple Methods Withing the Class

In Java, I have a class with a few methods that throw the same custom exception (the custom exception extends the 'Exception' class): private void setAColor(float r, float g, float b, float a) throws AnException {} private void setBColor(float r,…
Tanaki
  • 2,575
  • 6
  • 30
  • 41
0
votes
1 answer

Why is result of throwable get.Message ignored in my exception?

I made my own excpetions and I have to print the error message from the exception with a method that uses getMessage(). e.getMessage() is highlighted and it says result of Throwable.getMessage is ignored. For example if I input a number below 5 it…
0
votes
1 answer

Setter only does assignment, yet declares that it throws a ParseException; in Hibernate

Discovered a Date setter that only does the typical assignment, yet has a throws ParseException for what seems like no good reason. No Date construction is happening in the body of the setter. Just assignment. This is on a Hibernate entity. The…
candied_orange
  • 7,036
  • 2
  • 28
  • 62
0
votes
2 answers

How to pass differents Exceptions to a Java method?

So the idea is I have a bunch of methods separated in different 'repo' classes, each one of these can arise a personalized exception (all of them created by me, extending generic Java Exception). All these methods make a call to the same class, the…
Sr. X
  • 15
  • 6
0
votes
1 answer

Using throw new Error or try catch in Javascript async function

In below async Javascript function (which I use server side in Node js), I am doing a Graphql request using fetch api: import GET_DATA from './getData.gql'; const getData = async (url: string) => { const options = { method: 'POST', …
meez
  • 3,783
  • 5
  • 37
  • 91
0
votes
6 answers

Calling a method which throws FileNotFoundException

I'm pretty sure this is an easy one but I could not find a straight forward answer. How do I call a method with a throws FileNotFoundException? Here's my method: private static void fallingBlocks() throws FileNotFoundException
Switchkick
  • 2,716
  • 6
  • 21
  • 28
0
votes
1 answer

Generic throws clause handling in case of overridden methods

Consider the following clause from JLS: 8.4.8.3. If the unerased throws clause of m1 does not contain a supertype of each exception type in the throws clause of m2 (adapted, if necessary, to the type parameters of m1), a compile-time unchecked…
theutonium.18
  • 483
  • 2
  • 7
0
votes
0 answers

Is there a way to do variadic higher-kinded exception throws in Java?

basically the question is in the title. I'm looking for ways to set an interface method to throw 1..N java.lang.Exception subtypes. So far I'm only able to make it work using generic type parameters in the constructor like interface IFoo
laj0
  • 3
  • 1
0
votes
0 answers

Java Optional method orElseThrow doesn't throw anything despite having a null value inside

I have some consistency issue with the Optionals in Java. According to the documentation: public T orElseThrow​(Supplier exceptionSupplier) throws X extends Throwable If a value is present, returns the value, otherwise throws an…
0
votes
2 answers

Throw Exception in method who throws this Excpetion

I am doing a method who return an UUID of a Minecraft player from his username using the Mojang API. This method takes a String in parameter (the username of the player who we want to know the UUID). To use the resultat of the API, I use the…
Machigan
  • 17
  • 7
0
votes
4 answers

Can we say that the try-catch block is the only way to handle an exception in java?

So basically, if we use the throws keyword in a method signature, we are passing the handling responsibility to the caller method; if we use the throw keyword, we are explicitly throwing an exception. Both throws and throw are not ways to handle an…
kevinyyh
  • 1
  • 3
0
votes
3 answers

Return an arrayList as message in custom exception in JAVA

I need to output a List when an exception is thrown. I want the program to stop its execution once the exception is thrown. To make things clear, my code has these two functions: List exceptions = new ArrayList<>(); …
T.P.
  • 83
  • 3
  • 12
0
votes
1 answer

Eclipse IDE is catching an error in main however main itself has no issues?

[https://i.stack.imgur.com/iKE4t.png]] I have no code in my main method, or any separate classes. I'm still getting errors. public static void main(String[] args) { } However the debugger say main() is throwing…