Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/articles/java-tutorials/)
-   -   Exception handling with try-catch in Java (http://www.go4expert.com/articles/exception-handling-try-catch-java-t4148/)

Sanskruti 5May2007 17:09

Exception handling with try-catch in Java
 

Introduction



The code which is expected to generate the exception must be enclosed in the try and catch block. The very basic form of the exception-handling block is as following:
Code: Java

try
{
    //code which is expected to generate the exception
}
catch (<ExceptionTypeClass> object)
{
    //statements to handle the exception
}

Inside the try and catch block, as soon as the exception condition arises, an object of that specific type of exception is automatically created and thrown. The thrown object is then matched with the appropriate catch block, i.e. the catch block having the same parameter type as the object thrown. If the block matches, then the statements for handling the exception written within the catch block are executed.

Consider the following program, which demonstrates how the runtime environment handles an exception when it is generated. It is further modified to handle the exception through the code itself. The exception raised is the NumberFormatException. It arises when you try to convert a string to number format, and the string is either empty or does not contain the number in a proper format.
Code: Java

class NumFormExcp
{
    public static void main(String args[ ])
    {
        String str1 = new String("abc123");

        int num1 = Integer.parseInt(str1); //converts string to number

        System.out.println (num1);
    }
}

The output is:
Code:

Exception in thread "main" java.lang.NumberFormatException: For input string: "abc123"
        at java.lang.NumberFormatException.forInputString(Unknown Source)
        at java.lang.Integer.parseInt(Unknown Source)
        at java.lang.Integer.parseInt(Unknown Source)
        at NumFormExcp.main(NumFormExcp.java:7)

In the above program the string str1 does not store the number in an appropriate format, i.e., it contains a mix of text as well as numbers. When str1 is tried to be converted into a number, the object of type NumberFormatException is thrown. Since you are not catching this object, it is handled by the default exception handler that is the runtime environment. It prints the stack trace showing what is the exception and where it is generated.

The program given below shows, how you can handle this exception:
Code: Java

class NumFormExcp
{
    public static void main (String args [ ])
    {
        try
        {
            String str1 = new String ("abc123");
            int num1 = Integer.parseInt (str1);
            System.out.println (num1);

        }
        catch (NumberFormatException e)
        {
            System.out.println ("##Wrong Number##");
        }
    }
}

The output of the above program is:

##Wrong Number##

In the listing the NumberformatException is not left unhandled and there is a catch block ready to catch it. Once the exception is caught the statements inside the catch block execute, giving the appropriate message.

How to throw Exceptions in Java? >>


All times are GMT +5.5. The time now is 14:08.