Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do you have to write throws exception in a class definition?

Tags:

java

exception

Coming from C#, I just don't get this 'throws exception' that is written after a class/method definition:

public void Test() throws Exception

Do you have to write this? What if you don't? If I call a method that has this symbol, do I have to catch it?

like image 889
mrblah Avatar asked Jan 05 '10 19:01

mrblah


People also ask

Why do we write throws exception?

The throws keyword in Java is used to declare exceptions that can occur during the execution of a program. For any method that can throw exceptions, it is mandatory to use the throws keyword to list the exceptions that can be thrown.

What does it mean to throw an exception and throw statement?

The throw statement throws a user-defined exception. Execution of the current function will stop (the statements after throw won't be executed), and control will be passed to the first catch block in the call stack. If no catch block exists among caller functions, the program will terminate.

What happens when a class throws exception?

The caller has to handle the exception using a try-catch block or propagate the exception. We can throw either checked or unchecked exceptions. The throws keyword allows the compiler to help you write code that handles this type of error, but it does not prevent the abnormal termination of the program.


1 Answers

You don't have to write it in all cases -- you just have to write it if your method throws a checked Exception (an exception that is a subclass of Exception and not a subclass of RuntimeException). This is because your method signature is a contract, and it's declaring to all the code which calls it that it has the potential for throwing the given exception. Because it's a checked exception -- an exception which can be anticipated -- the calling code needs to anticipate the potential of seeing the exception thrown, and needs to be able to handle it.

To answer your two specific questions:

  • do you have to write it/what if you don't: if your method throws a checked exception, then yes, you have to declare it in your method signature. If you don't, then your code won't compile.
  • do you have to catch it: you have to do something with it. The code calling the method can either catch it and handle it, it can catch it and re-throw it, or it can just pass it up the chain. In order to pass it up the chain, the code calling the method has to itself declare that it throws the same exception -- e.g., if method bar can throw SomeException, and method foo calls bar and doesn't want to catch the exception, the method signature for foo would declare that it too throws SomeException.

The Exceptions chapter of the Java lessons is very good at explaining this in detail, and JavaWorld has a good article about throwing and catching exceptions that I've always found to be a good reference to pass onto folks.

like image 103
delfuego Avatar answered Sep 27 '22 20:09

delfuego