Exception handling with try-catch in Java

Discussion in 'Java' started by Sanskruti, May 5, 2007.

  1. Sanskruti

    Sanskruti New Member

    Joined:
    Jan 7, 2007
    Messages:
    108
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Consultant
    Location:
    Mumbai, India

    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:
    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:
    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:
    [COLOR=RoyalBlue]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)[/COLOR]
    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:
    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.

    [thread=4149]How to throw Exceptions in Java? >> [/thread]
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice