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
9
votes
4 answers

Catching versus Throwing Exceptions in Java

So I have two general questions about java in general. The first is when would one use a try/catch in the body of the method versus using throws exception in declaring the method? Here is a little demonstration of what I mean. This: public void…
Kemosabe
  • 311
  • 2
  • 4
  • 16
9
votes
2 answers

Can static code blocks throw exceptions?

In a hypothetical situation I have a class like this: import java.io.File; import java.util.Scanner; class X { static Scanner scanner; static { scanner = new Scanner(new File("X.txt")); } } When compiling, I get unreported…
The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75
7
votes
2 answers

Can I use multiple @throws tags for the same exception in Javadoc?

Can I use multiple @throws javadoc tags if my application throws the same exception for multiple reasons? For example: @throws UserException if issue 1 happened @throws UserException if issue 2 happened @throws UserException if issue 3 happened Is…
Victor Lyuboslavsky
  • 9,882
  • 25
  • 87
  • 134
7
votes
2 answers

Official recommendation / coding style guide on using multiple @throws tags for the same exception in JavaDoc

I just recently found out that one can use multiple @throws tags for the same exception in Javadoc. One of my students used it to document one of his methods in Connect Four: /* * ... * @throws IllegalArgumentException if the number of rows or…
Markus Weninger
  • 11,931
  • 7
  • 64
  • 137
7
votes
2 answers

Why is it not necessary to catch the IllegalArgumentException?

I wonder why the IllegalArgumentException class does not need to be catched or declared, while other exceptions have to (eg java.net.MalformedURLException). public void foo() { throw new IllegalArgumentException("spam"); } public void bar()…
Niklas R
  • 16,299
  • 28
  • 108
  • 203
7
votes
3 answers

Omitting throws declarations in derived classes

Consider the following interface: public interface Generator { String generate() throws IOException; } and the following implementation: public class EmptyStringGenerator implements Generator { @Override public String generate() { …
Jan Dörrenhaus
  • 6,581
  • 2
  • 34
  • 45
7
votes
1 answer

Convert ArrayList to Array throws java.lang.ArrayStoreException

i have a method convertToArray() which converts an ArrayList to an array. I want to call this method every time an element is added to the ArrayList. public class Table extends ArrayList { public String appArray[]; //Array of single applicant…
Hoggie1790
  • 319
  • 2
  • 7
  • 19
7
votes
1 answer

Changing the interface without recompiling the implementing class

I have the following classes public abstract interface X { public abstract void f() throws java.io.IOException; } public class Y implements X { public void f() throws java.io.IOException { throw new…
user93353
  • 13,733
  • 8
  • 60
  • 122
6
votes
4 answers

When should we use throws keyword in Java?

throws keyword is used only for checked exception. It instructs the caller to use try catch block to except all the listed exceptions by throws keyword. Since we know what kind of checked exception might occur in our module, then: Why don't we use…
Pran Kumar Sarkar
  • 953
  • 12
  • 26
6
votes
1 answer

'self' used inside 'catch' block reachable from super.init call

This code is not compiling on Swift 3.3. It's showing the message: 'self' used inside 'catch' block reachable from super.init call public class MyRegex : NSRegularExpression { public init(pattern: String) { do { try…
cristianomad
  • 303
  • 2
  • 9
6
votes
1 answer

How can I report exceptions in C#?

I have this code in Java, that I used to report exceptions (throws FileNotFoundException, IOException, ClassNotFoundException). Example: private void functionName() throws FileNotFoundException, IOException, ClassNotFoundException{} I need to do…
S.P
  • 73
  • 5
6
votes
5 answers

When to use "throws RuntimeException" Java

I am doing a code review and I have came across this method definition: public void something() throws RuntimeException Is there a rational reason to write 'throws RuntimeException' in Java?
MaKri
  • 190
  • 1
  • 8
6
votes
1 answer

Scala's @throws annotation is ignored in javac if I declare the variable as its abstract superclass

In Java you can't specify that an overridden abstract method throws some exception if the original abstract method does not (overridden method does not throw Exception.) However in Scala you can do this since it doesn't have checked exceptions.…
2rs2ts
  • 10,662
  • 10
  • 51
  • 95
5
votes
7 answers

Java - Difference between throwing an Exception and catching and rethrowing Exception

I'm confused as to what the beneift would be in catching and rethrowing an exception versus just throwing it in the first place. For example private void testMethod() throws Exception { //some bad code here } versus: private void testMethod()…
Michael878
  • 51
  • 1
  • 3
5
votes
2 answers

Why is main() method allowed to declare exceptions?

"Handle or declare. That's the law." - Head First But, is it a good law? Let me give an example first: public static void main(String[] args) throws Exception { m1(); } static void m1() throws Exception{ m2(); } static void m2() throws…
Stefan
  • 969
  • 6
  • 9
1 2
3
16 17