270 likes | 287 Views
Advanced Flow of Control : Introduction. This chapter focuses on: exception processing catching and handling exceptions creating new exceptions exception analysis. Exceptions( 예외상황 ). An exception is an object that describes an unusual situation
E N D
Advanced Flow of Control : Introduction • This chapter focuses on: • exception processing • catching and handling exceptions • creating new exceptions • exception analysis
Exceptions(예외상황) • An exception is an object that describes an unusual situation • Exceptions are thrown by a program or runtime environment, and • Thrown exceptions may be caughtand handledby another part
예외상황 발생 • Runtime exception • irrecoverable exception • thrown by the runtime library code, not your code • User-defined exception • by throw statement. • you have to make provision for catching thrown exceptions
예외상황 정의 • Exception object is a first-class object • 일반 object처럼 클래스를 이용하여 정의되고 • 일반 object처럼 사용될 수 있다. • 일반 object와 차이점 • throw 될 수 있다. • 예외상황을 정의하는 클래스 • Exception class를 상속받아서 정의해야 한다.
Object Throwable Exception Error RuntimeException ClassNotFoundException IllegalAcessException InstantiationException InterruptedException NoSuchMethodException ….. ArithmeticException NagativeArraySizeException ArrayIndexOutOfBoundsException SecurityException …
Execution Flow • A program can be separated into • a normal execution flow and • an exception execution flow
Exception Handling • A program can deal with an exception in one of three ways: (1) ignore it (2) handle it where it occurs (3) handle it an another place in the program
Exception Handling • If an exception is ignored by the program, • The program will terminate and produce a message • The message includes a call stack trace • main 메쏘드부터 호출과정 • See Zero.java
Example: Zero.java public class Zero { public static void main (String[] args) { int numerator = 10; int denominator = 0; System.out.println(numerator/denominator); // cann’t reach here System.out.println(“Cann’t reach here”); } // method main } // class Zero
The throw Statement • A programmer can define an exception by extending the appropriate class • Exceptions are thrown using throw statement: throw exception-object; • Usually a throw statement is nested inside an if statement
Example: Throw_Demo.java import java.io.IOException; public class Throw_Demo { public static void main (String[] args) throws Ooops { Ooops problem = new Ooops ("Alert!"); throw problem; // execution never gets to this point } // method main } // class Throw_Demo class Ooops extends IOException { Ooops (String message) { super (message); } // constructor Ooops } // class Ooops
The try-catch Statement • To process an exception when it occurs try { … } catch (E1 x) { … } … catch (En x) { … } • When an exception occurs in the try block, processing continues at the first catch clause that matches the exception type
Example: Adding.java import java.io.*; public class Adding { public static void main (String[] args) { int num1 = User_Reader.get_integer ("Enter a number:"); int num2 = User_Reader.get_integer ("Enter another number: "); System.out.println ("The sum is " + (num1+num2)); } // method main } // class Adding
class User_Reader { public static int get_integer (String prompt) { BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in)); int number = 0; boolean valid = false; while (! valid) { System.out.print (prompt); System.out.flush (); try { number = Integer.parseInt (stdin.readLine()); valid = true; } catch (NumberFormatException exception) { System.out.println ("Invalid input. Try again."); } catch (IOException exception) { System.out.println ("Input problem. Terminating."); System.exit(0); } } return number; } // method get_integer } // class User_Reader
The finally Clause • A try statement can have an optional clause finally try { … } catch (E1 x) { … } … catch (En x) { … } finally { … } • The statements in the finally clause are executed whether exception is generated or not
Example: ThrowDemo.java class ThrowDemo{ public static void main(String arg[]){ try{ System.out.println("a() 메서드 이전"); a(); System.out.println("a() 메서드 이후"); } catch(Exeception e){ System.out.println("main:" + e); } finally{ System.out.println("main:finally 블록"); } }
public static void a(){ try{ System.out.println("throw문 이전"); throw new ArithmeticException(); } catch(Exception e){ System.out.println("a: " + e); } finally{ System.out.println("a:finally 블록"); } }//method main }//class ThrowDemo
Exception Propagation • Exceptions propagate up through the method calling hierarchy • until they are caught and handled or • until they reach the outermost level
Exception Propagation Chain of call at runtime Main() Apple(); Orange()
Source program 6 Then here enclosing the call to apple() Main(){ apple(); } apple(){ orange(); } orange(){ int i, j =0; ... i=i/j; } 5 Then here surrounding the call to orange() 4 3 Runtime system look for try…catch statement in orange 2 Exception occur! 1
Example: Propagation_Demo.java public class Propagation_Demo { static public void main (String[] args) { Exception_Scope demo = new Exception_Scope(); System.out.println("program beginning"); demo.level1(); System.out.println("program ending"); } // method main } // class Propagation_Demo
class Exception_Scope { public void level3 (int adjustment) { int current = 1; System.out.println("level3 beginning"); current = current / adjustment; System.out.println("level3 ending"); } // method level3 public void level2() { System.out.println("level2 beginning"); level3 (0); System.out.println("level2 ending"); } // method level2 public void level1() { System.out.println("level1 beginning"); try { level2(); } catch (ArithmeticException problem) { System.out.println (problem.getMessage()); problem.printStackTrace(); } System.out.println("level1 ending"); } // method level1 } // class Exception_Scope
Exceptions • An exception is either checked or unchecked • The compiler will complain if a checked exception is not caught appropriately • An unchecked exception does not require explicit handling • A checked exception can only be thrown • within a try block or • within a method specified to throw the exception
Specification of uncaught exceptions • Specify uncaught exceptions in method definition m(...) throws A, B, …, C { … }
Example: Spec.java import java.io.*; public class Spec{ public static void main (String[] args)throws IOException{ System.out.println("jylee....!!"); int ch = System.in.read(); System.out.println((char)ch); } }
Uncaught Exception Analysis in JDK • Intraprocedural analysis • Based on programmer’s specifications of method definitions. • Check whether thrown exceptions are caught or specified in the method definition