MY mENU


Friday 10 February 2012

An Exception Thrown By a method without Handeling and Handling


Sometimes, it's appropriate for code to catch Exceptions that can occur within it. In other cases, however, it's better to let a method further up the call stack Handle the Exception. In that situation it's better to throw an Exception without handle (catch) it.

- It's done by Throws clause

Throws clause : It comprises the throws keyword followed by a comma separated list of all the Exceptions thrown by that method. The clause goes after the method name and argument list and before the brace that defines the scope of the method.

Note : we can use Throws clause only for Checked Exceptions. i.e., like IOExcepion, FileNotFoundException.

Example :

public void writeList() throws IOException { ---- }

Throwing an Exception:


Creating an Exception Object and handing it to the Runtime System is called Throwing an Exception.


We can catch and handle Exceptions by using these three Exception Handler Components

1. Try Block
2. Catch Block
3. Finally Block

No comments:

Post a Comment