follow

Friday, 9 August 2013

Use of "finally" for handling the Exception block :JAVA Programming by "Gopal Krishna"


The finally keyword is used to create a block of code that follows a try block. A finally block of code always executes, whether or not an exception has occurred.
Using a finally block allows you to run any cleanup-type statements that you want to execute, no matter what happens in the protected code.

A finally block appears at the end of the catch blocks and has the following syntax:


try 
{
   //Protected code
}catch(ExceptionType1 e1)
{
   //Catch block
}catch(ExceptionType2 e2)
{
   //Catch block
}catch(ExceptionType3 e3)
{
   //Catch block
}finally
{
   //The finally block always executes.

}




public class ab
{
public static void main(String arg[])
{
System.out.println("Enter the amount"+arg[0]);
try
{
int a=Integer.parseInt(arg[0]);
if (a>100)
{
System.out.println("valid amount");
}

}
catch(Exception e)
{
System.out.println("enter the valid amount which is more then 100");
}
finally
{
System.out.println("thnks you for using our servie");
}

}
}

No comments:

Post a Comment