Exception Handling BSC (CS)
Exception Handling BSC (CS)
java.lang.ArithmeticException: / by zero
at Exc0.main(Exc0.java:4)
OUTPUT
Exception: java.lang.ArithmeticException: / by zero
C:\>java MultipleCatches
a=0
Divide by 0: java.lang.ArithmeticException: / by zero
After try/catch blocks.
C:\>java MultipleCatches TestArg
a=1
Array index oob:
java.lang.ArrayIndexOutOfBoundsException:42
After try/catch blocks.
Rakhi Saxena (Internet Technologies) 13
Nested try Statements
• a try statement can be inside the block of another try
• Each time a try statement is entered, the context of
that exception is pushed on the stack
• If an inner try statement does not have a catch handler
for a particular exception, the stack is unwound and
the next try statement’s catch handlers are inspected
for a match
• This continues until one of the catch statements
succeeds, or until all of the nested try statements are
exhausted
• If no catch statement matches, then the Java run-time
system will handle the exception
C:\>java NestTry
Divide by 0: java.lang.ArithmeticException: / by zero
C:\>java NestTry One
a=1
Divide by 0: java.lang.ArithmeticException: / by zero
C:\>java NestTry One Two
a=2
Array index out-of-bounds:
java.lang.ArrayIndexOutOfBoundsException:42