Throwing Exception

The throw Keyword
A program can explicitly throw an exception using the throw statement besides the implicit exception thrown
The general format of the throw statement is as follows:

throw ThrowableInstance
ThrowableInstance must be an object of type Throwable or a subclass of Throwable.
2 ways to obtain Throwable object:
Using a parameter into a catch clause

Creating one with a new operator.

try {

throw new NullPointerException("created");

}catch (NullPointerException e) {

throw e; //rethrow the Exception

The throws Keyword
throws " is a keyword defined in the Java programming language. Keywords are basically reserved words which have specific meaning relevant to a compiler in Java programming language likewise the throw keyword indicates the following :
--- The throws keyword in Java programming language is applicable to a method to indicate that the method raises particular type of exception while being processed.
--- The throws keyword in Java programming language takes arguments as a  list of the objects of type java.lang.Throwable class.
--- when we use the throws with a method it is known as ducking. The  method calling a method with a throws clause is needed to be enclosed within the try catch blocks.
This is the general form of a method declaration that includes a throws clause:
         type method-name(parameter-list) throws exception-list

       // body of method

People who read this post also read :


Post a Comment


Twitter Delicious Facebook Digg Stumbleupon Favorites More