Programming Java: Exception Handling
Programming Java: Exception Handling
Exception Handling
Incheon Paik
Java
Contents
Exception Handling Catch Block Searches The throw Statement Exception and Error Classes The throws Clause Custom Exceptions
Java
Exception Handling
class DivideByZero { public static void main(String args[]) { a(); } static void a() { b(); } static void b() { c(); } static void c() { d(); } static void d() { int i = 1; int j = 0; System.out.println(i / j); } }
Result :
Exception in thread "main" java.lang.ArithmeticException: / by zero at DivideByZero.d(DivideByZero.java:22) at DivideByZero.c(DivideByZero.java:16) at DivideByZero.b(DivideByZero.java:12) at DivideByZero.a(DivideByZero.java:8) at DivideByZero.main(DivideByZero.java:4)
Java
Exception Handling
Exception Handling
try { // try block } catch (ExceptionType1 param1) { // Exception Block } catch (ExceptionType2 param2) { // Exception Block }
Do always
Java
Exception Handling
class Divider { public static void main(String args[]) { try { System.out.println("Before Division"); int i = Integer.parseInt(args[0]); int j = Integer.parseInt(args[1]); System.out.println(i / j); System.out.println("After Division"); } catch (ArithmeticException e) { System.out.println("ArithmeticException"); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("ArrayIndex" + "OutOfBoundsException"); } catch (NumberFormatException e) { System.out.println("NumberFormatException"); } finally { System.out.println("Finally block"); } } }
Java
Result :
Before a Before b Before c Before d d: finally c: finally b: java.lang.ArrayIndexOutOfBoundsException: 10 b: finally After b a: finally After a main: finally
Java
throw param;
..
Java
Result :
Before a Before b Before c Before d Before division d: java.lang.ArithmeticException: / by zero d: finally c: java.lang.ArithmeticException: / by zero c: finally b: java.lang.ArithmeticException: / by zero b: finally After b a: finally After a main: finally
Java
Result :
Java
Subclasses of Exception Class ClassNotFoundException IllegalAccessException InstantiationException InterruptedException NoSuchFieldException NoSuchMethodException RuntimeException Subclasses of RuntimeException ArrayIndexOutOfBoundsException ArithmeticException ClassCastException NegativeArraySizeException NullPointerException NumberFromatException SecurityException StringIndexOutOfBoundsException
getMessage() Method
String getMessage()
printStackTrace() Method
void printStackTrace()
Exception Constructor
Exception() Exception(String message)
Java
10
Result:
java.lang.ArithmeticException: / by zero at PrintStackTraceDemo.d(PrintStackTraceDemo.java:43) at PrintStackTraceDemo.c(PrintStackTraceDemo.java:32) at PrintStackTraceDemo.b(PrintStackTraceDemo.java:23) at PrintStackTraceDemo.a(PrintStackTraceDemo.java:14) at PrintStackTraceDemo.main(PrintStackTraceDemo.java:5) 11 Computer Industry Lab.
Java
class ThrowsDemo { public static void main(String args[]) { a(); } public static void a() { try { b(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } 12
Java
Custom Exceptions
Subclass of Exception
import java.util.*; class ExceptionSubclass { public static void main(String args[]) { a(); } static void a() { try { b(); } catch (Exception e) { e.printStackTrace(); } } static void b() throws ExceptionA { try { c(); } catch (ExceptionB e) { e.printStackTrace(); } } static void c() throws ExceptionA, ExceptionB { Random random = new Random(); int i = random.nextInt(); if (i % 2 == 0) { throw new ExceptionA("We have a problem"); } else { throw new ExceptionB("We have a big problem"); } } } class ExceptionA extends Exception { public ExceptionA(String message) { super(message); } } class ExceptionB extends Exception { public ExceptionB(String message) { super(message); } }
Execution:
ExceptionA: We have a problem at ExceptionSubclass.c(ExceptionSubclass.java:31) at ExceptionSubclass.b(ExceptionSubclass.java:20) at ExceptionSubclass.a(ExceptionSubclass.java:11) at ExceptionSubclass.main(ExceptionSubclass.java:6)
Java
13
Exercise
Step 1 (Creating Some Methods for Exception)
Slides 7,8,9, 12
static int thrower(String s) ...... { try { if (s.equals("divide")) { // Write the code for raising Divide by Zero Exception } if (s.equals("null")) { // Write the code for raising NullPoint Exception } if (s.equals("test")) // Write the code for raising TestException return 0; } finally { // Some code for print out the current message }
Java
14
Exercise
Java
15