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
28
votes
3 answers

Python : Java throws equivalent in python

Not attempting to compare the languages but just for knowledge, Is there any way to have equivalent of java throws keyword/functionality in Python? or the way we can recognize checked exception thrown by any method at static time? or…
Nikhil Rupanawar
  • 4,061
  • 10
  • 35
  • 51
25
votes
6 answers

What happens if a method throws an exception that was not specified in the method declaration with "throws"

I've never used the "throws" clause, and today a mate told me that I had to specify in the method declaration which exceptions the method may throw. However, I've been using exceptions without problems without doing it, so, why is it needed if, in…
bluehallu
  • 10,205
  • 9
  • 44
  • 61
21
votes
3 answers

How can I throw an exception for the whole class instead of doing it method by method

I'm writing a program in Java, and nearly every method in one of my classes is written like: public void doStuff() throws AWTException{} Is there a way for me to get rid of the extra step of typing throws AWTException for each method, and somehow…
Dream Lane
  • 1,282
  • 5
  • 16
  • 27
20
votes
4 answers

Inheritance , method signature , method overriding and throws clause

My Parent class is : import java.io.IOException; public class Parent { int x = 0; public int getX() throws IOException{ if(x<=0){ throw new IOException(); } return x; } } I extend this class to…
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
18
votes
5 answers

Should I put throws IllegalArgumentException at the function?

I'm building a scientific software with lots of calculations and of course arguments can have wrong lengths etc... So I used IllegalArgumentException class as it seemed right name for the issue, but should I put the throws IllegalArgumentException…
Ismail Marmoush
  • 13,140
  • 25
  • 80
  • 114
17
votes
3 answers

Difference between throw and throws in Java?

Can any one clearly state the difference between throw and throws in Java exception handling with an example? I have tried googling but couldn't arrive at a conclusion. Pls help
user3527594
  • 181
  • 1
  • 1
  • 5
14
votes
3 answers

Scaladoc (2.11.6) fails on throws tag with "unable to find any member to link"

When attempting to publish the jars for my project via sbt "++2.11.6 publishLocal" or sbt +publishLocal, I encounter Scaladoc issues when publishing for Scala 2.11.6. It appears that I have invalid links caused by the @throws tag. I am not sure why…
Senkwich
  • 1,022
  • 1
  • 9
  • 17
13
votes
1 answer

Good pattern? ... method() throws X

Some background, then some questions. I have only recently discovered that an interface (or class) may be generic in the type of (checked) exception that its methods may throw. For example: interface GenericRunnable { void…
user2357
  • 452
  • 5
  • 11
12
votes
2 answers

Why do either of these rethrown exceptions create a compiler error?

Why does throw outerE; generate a compilation error? I know that throw e; should not generate a compiler error because of the precise rethrow feature. They're the same Exception object, but one is scoped inside the catch block only and one is…
Venkata Raju
  • 4,866
  • 3
  • 27
  • 36
11
votes
2 answers

Catching an Exception and rethrowing it, but it's not an Exception

I stumbled upon code looking something like this: void run() { try { doSomething(); } catch (Exception ex) { System.out.println("Error: " + ex); throw ex; } } void doSomething() { throw new…
Simon Forsberg
  • 13,086
  • 10
  • 64
  • 108
11
votes
1 answer

Java checked exception not in the function's throw specification?

Normally, the Java compiler confirms that all checked exceptions that are thrown are in the throw specification. Does anything special happen when a native function throws a java checked exception that was not in the functions throw specification…
Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
10
votes
4 answers

How to define an interface throwing a generic exception type?

I wanna define an interface, like public interface Visitor { public ResultType visitProgram(Program prog, ArgType arg) throws SelfDefinedException; //... } during implementation,…
CrepuscularV
  • 983
  • 3
  • 12
  • 25
10
votes
1 answer

Why is there not a no-throw guarantee in the standard for std::set::extract() and std::set::insert(nh)?

In C++20 (N4849), there is no exception safety wording for associative containers' extract() and insert(node_handle)/insert(hint, node_handle) methods. But for merge(), there is this wording though: Throws: Nothing unless the comparison object…
zwhconst
  • 1,352
  • 9
  • 19
10
votes
4 answers

How to verify that all own thrown runtime exceptions are covered in Javadoc?

I throw a bunch of custom runtime exceptions in my code and I want to make sure that in all public methods, I document which runtime exception might be thrown (by myself) and why. This would be very hulpful since I'm maintaining a library which is…
Benny Bottema
  • 11,111
  • 10
  • 71
  • 96
9
votes
2 answers

How do I declare a function parameter to accept functions that throw?

I have defined a function in Kotlin: fun convertExceptionToEmpty(requestFunc: () -> List): Stream { try { return requestFunc().stream() } catch (th: Throwable) { // Log the exception... return…
Michael Richardson
  • 4,213
  • 2
  • 31
  • 48
1
2
3
16 17