Total members 11889 |It is currently Thu Mar 28, 2024 6:12 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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.



_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  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     -  
 What is Java API?!!!     -  
 java or .net     -  
 need help in java     -  
 Using FTP in java     -  
 what is java     -  



Topic Tags

Java Exceptions
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com