1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.

Slides:



Advertisements
Similar presentations
Pearson Education, Inc. All rights reserved. 1.. Exception Handling.
Advertisements

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Exceptions Ensuring program reliability. Program correctness The term program correctness refers to a program’s working as advertised; that is, it produces.
CS102--Object Oriented Programming
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
© 2004 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 : Exceptions Intermediate Java Programming Summer 2007.
1 Chapter 10: Exceptions and I/O Streams Original Slides by John Lewis and William Loftus Modified significantly by Bob Roggio.
Java I/O Java I/O is based on input streams and output streams. All input and output are defined in the Java IO package. 1.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
Java Exceptions. Exceptions Often in computing, operations cannot properly execute because some sort of error has occurred. Some examples: Often in computing,
Exception Handling1. 2 Exceptions  Definition  Exception types  Exception Hierarchy  Catching exceptions  Throwing exceptions  Defining exceptions.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Exceptions Briana B. Morrison CSE 1302C Spring 2010.
Exception Handling. Introduction An exception is an abnormal condition that arises in a code sequence at run time. In computer languages that do not support.
File I/O and Exceptions File I/O Exceptions Throwing Exceptions Try statement and catch / finally clauses Checked and unchecked exceptions Throws clause.
Java Programming Transparency No. 1-1 Java Exception Handling Cheng-Chia Chen.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exceptions Handling.
Exceptions Used to signal errors or unexpected situations to calling code Should not be used for problems that can be dealt with reasonably within local.
1 Exception Handling  Introduction to Exceptions  How exceptions are generated  A partial hierarchy of Java exceptions  Checked and Unchecked Exceptions.
Exceptions and Assertions Recitation – 03/13/2009 CS 180 Department of Computer Science, Purdue University.
Java Programming Transparency No. 1-1 Lecture 3.3. Java Exception Handling Cheng-Chia Chen.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
1 Lecture#8: EXCEPTION HANDLING Overview l What exceptions should be handled or thrown ? l The syntax of the try statement. l The semantics of the try.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Java Software Solutions Foundations of Program Design Sixth Edition
Preventing and Correcting Errors
1 Advanced Flow of Control - Introduction - zTwo additional mechanisms for controlling process execution are exceptions and threads zChapter 14 focuses.
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
Java Programming Exception Handling. The exception handling is one of the powerful mechanism provided in java. It provides the mechanism to handle the.
Java Software Solutions Lewis and Loftus Chapter 14 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Advanced Flow of Control --
1 Lecture 11(chap 14) Exception Handling & Thread Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute.
Chapter 10 Exceptions. Chapter Scope The purpose of exceptions Exception messages The call stack trace The try-catch statement Exception propagation The.
Exception Handling in JAVA. Introduction Exception is an abnormal condition that arises when executing a program. In the languages that do not support.
Java I/O Java I/O is based on input streams and output streams. All input and output are defined in the Java IO package. 1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Exceptions Chapter 10 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
© 2004 Pearson Addison-Wesley. All rights reserved April 24, 2006 Exceptions (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING 2006.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
1 Chapter 10: Exceptions and I/O Streams Original Slides by John Lewis and William Loftus Modified significantly by Bob Roggio, July 2007.
Exceptions in Java. What is an exception? An exception is an error condition that changes the normal flow of control in a program Exceptions in Java separates.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Agenda Introduction Errors and Exception Exception Hierarchy Classification of Exceptions Built in Exceptions Exception Handling in Java User defined.
CSE 1201 Object Oriented Programming
Chapter 10 – Exception Handling
04/14/14 Exceptions.
ATS Application Programming: Java Programming
Exceptions Exception handling is an important aspect of object-oriented design Chapter 10 focuses on the purpose of exceptions exception messages the.
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Abdulmotaleb El Saddik University of Ottawa
Exception Handling Chapter 9 Edited by JJ.
Web Design & Development Lecture 7
Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013
Java Programming Exceptions CSC 444 By Ralph B. Bisland, Jr.
Exception Handling Contents
Exceptions (part 2) December 3, 2007 ComS 207: Programming I (in Java)
Chapter 11 Exceptions Java Software Solutions
Java Basics Exception Handling.
Presentation transcript:

1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception analysis

2 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 caught and handled by another part

3 예외상황 발생 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

4 예외상황 정의 Exception object is a first-class object – 일반 object 처럼 클래스를 이용하여 정의되고 – 일반 object 처럼 사용될 수 있다. 일반 object 와 차이점 – throw 될 수 있다. 예외상황을 정의하는 클래스 –Exception class 를 상속받아서 정의해야 한다.

5 Object Throwable Exception Error RuntimeException ClassNotFoundException IllegalAcessException InstantiationException InterruptedException NoSuchMethodException ….. ArithmeticException NagativeArraySizeException ArrayIndexOutOfBoundsException SecurityException …

6 Execution Flow A program can be separated into –a normal execution flow and –an exception execution flow

7 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

8 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

9 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 Example: Zero.java

10 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

11 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

12 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

13 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

14 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

15 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

16 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 블록 "); } Example: ThrowDemo.java

17 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

18 Exception Propagation Exceptions propagate up through the method calling hierarchy –until they are caught and handled or –until they reach the outermost level

19 Exception Propagation Chain of call at runtime Main() Apple(); Orange()

20 Source program Main(){ apple(); } apple(){ orange(); } orange(){ int i, j =0;... i=i/j; } Exception occur! Runtime system look for try…catch statement in orange Then here surrounding the call to orange() Then here enclosing the call to apple()

21 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

22 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

23 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

24 Specification of uncaught exceptions Specify uncaught exceptions in method definition m(...) throws A, B, …, C { … }

25 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); } Example: Spec.java

26 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