Java Programming: Exception Handling
Java Programming: Exception Handling
Exception Handling
Incheon Paik
try {
The codes that have some
// try block possibility to happen exception
}
catch (ExceptionType1 param1) {
// Exception Block
}
catch (ExceptionType2 param2) { Carry out these codes when the
// Exception Block corresponding exception occurred.
}
……
catch (ExceptionTypeN paramN) {
// Exception Block
}
finally { Do always
// finally Block
}
System.out.println("Before Division");
int i = Integer.parseInt(args[0]);
int j = Integer.parseInt(args[1]);
Result : java Divider
System.out.println(i / j);
System.out.println("After Division"); Before Division
} ArrayIndexOutOfBoundsException
catch (ArithmeticException e) { Finally block
System.out.println("ArithmeticException");
}
catch (ArrayIndexOutOfBoundsException e) { Result : java Divider 1 0
System.out.println("ArrayIndex" + Before Division
"OutOfBoundsException");
ArithmeticException
}
catch (NumberFormatException e) { Finally block
System.out.println("NumberFormatException");
}
Subclasses of RuntimeException
printStackTrace() Method ArrayIndexOutOfBoundsException
class ThrowsDemo {
public static void main(String args[]) {
a();
}
public static void a() {
try {
b();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
} // end of while