written 7.9 years ago by | • modified 7.9 years ago |
1)Try-catch
2)Finally keyword
3)Catching multiple exception
4)Throwing exception
Marks: 10 M
Year: Dec 2013, Dec 2014
written 7.9 years ago by | • modified 7.9 years ago |
1)Try-catch
2)Finally keyword
3)Catching multiple exception
4)Throwing exception
Marks: 10 M
Year: Dec 2013, Dec 2014
written 7.9 years ago by | • modified 7.9 years ago |
1) Try-Catch:
The general syntax of Try-catch is as follows:
try
{
// block of code to monitor for errors
}
catch (ExceptionType1 exOb)
{
// exception handler for ExceptionType1
}
catch (ExceptionType2 exOb)
{
// exception handler for ExceptionType2
}
//.......
Here, ExceptionType is the type of exception that has occurred and exOb.
2) Finally Keyword:
The try-catch-finally of try-finally construct can be created as,
try
{
.......
}
catch(....) //multiple catch are allowed
{
.......
}
finally
{
.......
}
OR
try
{
.......
}
finally
{
.......
}
The program below illustrates use of finally keyword:
//Use of finally statement
class FinallyClause
{
public static void main(String args[])
{
try
{
//int val = 0; //statement1
//int m = 100 / val; //statement2
int []x = new int[-5]; //statement3
System.out.println("No output");
}
catch(ArithmeticException e)
{
System.out.println("Exception: "+e);
}
finally
{
System.out.println("Program end");
System.out.println("Bye bye...");
}
}
}
Output:
Program end
Bye bye...
Exception in thread "main"
java.lang.NegativeArraySizeException
at FinallyClause.main(FinallyClause.java:10)
In program, the exception of “negative array size” has occurred at ststement3. But the catch statement will not catch it as only ArithmeticException is mentioned there. So the program will be terminated. But before its termination and printing the stack trace, the finally block is executed.
3) Catching multiple exception
The following example traps two different exception types:
// Using multiple catch statements
import java.util.Scanner;
class MultiCatch
{
public static void main(String args[])
{
int x,z;
Scanner in = new Scanner(System.in);
System.out.print("Enter number : ");
x = in.nextInt();
try
{
z = 50 / x; //statement1
System.out.println("Division: "+z);
short arr[] = {5,7,8};
arr[10] = 120; //statement2
System.out.println("Try end...");
}
catch(ArithmeticException e)
{
System.out.println("Division by zero");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array indexing wrong");
}
System.out.println("Program end...");
}//end of main()
}//end of class
Output:
Enter number: 0
Division by zero
Program end...
Enter number: 5
Division: 10
Array indexing wrong
Program end...
4) Throwing exception
need to derive our class from Exception.
// Creating our own exception.
class NegativeOutputException extends Exception
{
private int det;
NegativeOutputException(int a)
{
det = a;
}
public String toString()
{
return "NegativeOutputException["+det+"]";
}
}
class OwnException
{
public static void main(String args[])
{
int x = Integer.parseInt(args[0]);
int y = Integer.parseInt(args[1]);;
int z;
try
{
z = x * y;
if(z<0) //statement1
throw new NegativeOutputException(z);
System.out.println("Output: "+z);
}
catch (NegativeOutputException e)
{
System.out.println("Caught: "+e);
}
}
}
Output:
java OwnException 4 8
Output: 32
java OwnException 4 -3
Caught: NegativeOutputException[-12]
java OwnException -4 -3
Output: 12