1 ָ נן oop uException-Handling Mechanism – similar to C++ l throw e signals the occurrence of an exception lThe statement try/catch allows the calling.

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
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.
Java Exception Very slightly modified from K.P. Chow University of Hong Kong (some slides from S.M. Yiu)
Index Exception handling Exception In Java Exception Types
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
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.
Understanding Java Exceptions. Outline What exceptions are for What exceptions are NOT for Catching & Throwing exceptions Exception Specifications Standard.
MIT-AITI Lecture 14: Exceptions Handling Errors with Exceptions Kenya 2005.
EXCEPTIONS. What’s an exception?? Change the flow of control when something important happens ideally - we catch errors at compile time doesn’t happen.
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.
Lecture 28 More on Exceptions COMP1681 / SE15 Introduction to Programming.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
1 Why do we need exceptions? In C, return variables must be used to indicate errors: if((fd = fopen(path,...)) == -1){ if(errno==a){...} else if(errno==b){...}
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
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.
交通大學資訊工程學系 Programming in Java Exception Handling again 蔡文能 交通大學資訊工程學系
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java Exceptions. Intro to Exceptions  What are exceptions? –Events that occur during the execution of a program that interrupt the normal flow of control.
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.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
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.
Example 1 :- Handling integer values public class Program1 { public static void main(String [] args) { int value1, value2, sum; value1 = Integer.parseInt(args[0]);
Java Software Solutions Foundations of Program Design Sixth Edition
Preventing and Correcting Errors
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Chapter 12 Inheritance and Exceptions Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,
Object Oriented Programming
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.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Java Programming Exception Handling. The exception handling is one of the powerful mechanism provided in java. It provides the mechanism to handle the.
Slides Credit Umair Javed LUMS Web Application Development.
COMP Exception Handling Yi Hong June 10, 2015.
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.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Exceptions Handling Exceptions with try and catch The finally-block The throws.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
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.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
Exceptions Handling Prepared by: Ligemm Mae del Castillo.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
Java Programming: Exceptions1 Exceptions Reference: java.sun.com/docs/books/tutorial/essential/exceptions/
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Introduction to Exceptions in Java CS201, SW Development Methods.
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
Agenda Introduction Errors and Exception Exception Hierarchy Classification of Exceptions Built in Exceptions Exception Handling in Java User defined.
Exception Handling. You learned that there are three categories of errors: syntax errors, runtime errors, and logic errors. Syntax errors arise because.
Java Exceptions a quick review….
ATS Application Programming: Java Programming
Exception Handling and Reading / Writing Files
Web Design & Development Lecture 7
Lecture 11 Objectives Learn what an exception is.
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
Chapter 12 Exception Handling and Text IO Part 1
Why do we need exceptions?
Java Basics Exception Handling.
Java Programming: From Problem Analysis to Program Design, 4e
Design by Contract – Exceptions
Presentation transcript:

1 ָ נן oop uException-Handling Mechanism – similar to C++ l throw e signals the occurrence of an exception lThe statement try/catch allows the calling method to “catch” the “thrown” exception object Exceptions in Java public static void main(String[] args) try{ // Open a file: FileReader f = new FileReader(args[0]); System.out.println(args[0] + " opened"); f.close(); } catch (IOException x){ System.out.println(x); }

2 ָ נן oop The finally Clause uFor code that must ALWAYS run lRegardless of which catch block executed lEven if no catch block executed lEven if a return or break occurs first lException: System.exit( ) lExecutes before transferring control to caller uPlaced after handlers (if they exist) ltry-block must either have a handler or a finally-block ucan be used to clean up the programming environment void n() { try { … open window … p() … } catch (SomeException se) { … } finally { … close window … } } void p() { … throw se … }

3 ָ נן oop Throwing Exceptions uThrown objects must derive (ultimately) from Throwable lProvides methods like printStackTrace() uUsually derive from java.lang.Exception uTry can catch any exception using the following code: uBe careful: Just like C++, Java executes the first catch statement capable of handling the exception try { … } catch (Exception e) { … handle any type of exception … } try { … } catch (Exception e) { … } catch (ArrayIndexOutOfBounds ae) { … } the second handler is never executed

4 ָ נן oop Exception Hierarchy InterruptedException ArithmeticException Throwable Object ParseException IOException Exception RunTimeException IndexOutOfBoundsException ArithmeticException NullPointerException

5 ָ נן oop Checked vs. Unchecked Exceptions uChecked exceptions lErrors a program should handle lAre subclasses of Exception lCompiler requires “catch or declare” Catch and handle the exception in method, OR Declare method can throw exception void A() throws ExceptionType { … } uUnchecked exceptions lRepresent defects in the program lAre subclasses of RuntimeException lMethods are not required to declare or handle

6 ָ נן oop Exceptions and Inheritance uMethods overridden in subclasses must maintain the parent method’s contract lsubstitutability lcannot add exceptions to specification lcan omit, however lcan throw subclasses of parent’s exceptions class Parent{ public void f() throws Exception {} } class Child extends Parent{ public void f()// OK! {} }

7 ָ נן oop Quiz public void m() throws Exception{ try { System.out.println("m's throw"); throw new Exception(); } catch(Exception E){ System.out.println("m's handler"); throw E; } finally{ System.out.println("m's finally"); } public void n() { try { System.out.println("n calling m"); m(); } catch(Exception E){ System.out.println( “ n's handler"); } finally{ System.out.println( “ n's finally"); } What is the output of a call to n() ?

8 ָ נן oop Exceptions in Eiffel uEiffel is based on a “design by contract” concept lRoutine preconditions - must be true upon calling a method. lRoutine postconditions - must be true at the end of a method. lClass invariants - must be satisfied at all stable times. uA routine that is unable to meet its contract fails. lThe failure of an operation is an exception for the routine that needed the operation. uThe rescue keyword defines an exception handler lWithin a rescue block one can retry the routine. retry starts again the execution of the routine, without repeating the initialization of local entities uIf a precondition is not met, the rescue block must be defined in the calling routine.

9 ָ נן oop An Example read_next_character(f: FILE) is -- Make next character available in last_character; require readable: file.readable local attempts: INTEGER do last_character:= low_level_read_function(f) rescue attempts:= attempts + 1 if attempts < Max_attempts then retry end if false, read_next_character fails and the exception must be handled in the calling routine the file exists and is accessible for reading

10 ָ נן oop Programmer Exceptions uA class that derives from class Exceptions can explicitly trigger exceptions l raise (name: STRING) - raises a developer exception of name name l developer_exception_name : STRING – retrieves the name of last developer-raised exception to detect and process specific exceptions.