Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

How to throw Exceptions in Java?

Sun Oct 12, 2008 7:31 am

Before you can catch an exception, some code somewhere must throw one. Any code can throw an exception: your code, code from a package written by someone else such as the packages that come with the Java platform, or the Java runtime environment. Regardless of what throws the exception, it's always thrown with the throw statement.

Java platform provides numerous exception classes. All the classes are descendants of the Throwable class, and all allow programs to differentiate among the various types of exceptions that can occur during the execution of a program.You can also create your own exception classes to represent problems that can occur within the classes you write.

The throw Statement


All methods use the throw statement to throw an exception. The throw statement requires a single argument: a throwable object. Throwable objects are instances of any subclass of the Throwable class. Here's an example of a throw statement.
Code:
throw someThrowableObject;


If you want to throw an exception explicitly then you need to use the throw clause. All the system-defined exceptions are thrown automatically, but the user-defined exceptions must be thrown explicitly using the throw clause. It takes the form as:
Code:
try
{
     // statements     throw new UserDefinedException( );     // statements
}
catch (UserDefinedException e)
{
     System.out.printIn ("User defined exception caught");
}


The UserDefinedException is a class made specifically to handle an exception and it is encapsulating some specific kind of user defined behavior. This exception is raised explicitly using the throw clause. At the same time the catch clause is there ready to catch the same exception object.

Consider the following example in which a system defined exception is deliberately thrown in order to demonstrate the functionality of the throw clause:
Code:
class Throw_clause
{
    public static void main (String args [ ])
    { 
       try
         {
             throw new NullPointerException ( );
        }
catch (NullPointerException e)   
     {       
     System.out.println ("Invalid reference use");   
      }
    }
}


the output is :
Code:
Invalid reference use


In the above example the throw clause throws an object of a system defined exception NullPointerException, which is actually thrown when an object storing a null pointer is reference. Thus, as soon as an the exception is raised it is caught with the catch clause, the statement inside the catch clause are executed and we get the above output.



Post a reply
  Related Posts  to : How to throw Exceptions in Java?
 Creating your own Exceptions in Java     -  
 Difference between throw and throws     -  
 What are exceptions and how to handle them!!!     -  
 exceptions catch by a catch clause     -  
 2d game in java-Monster-Java 2D Game Graphics and Animation     -  
 need help in java     -  
 Using FTP in java     -  
 what is java     -  
 Java course     -  
 What is Java API?!!!     -  

Topic Tags

Java Exceptions