Chapter 13 Exception Handling

Slides:



Advertisements
Similar presentations
Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Advertisements

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 13 Exception.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
Introduction to Java Chapter 11 Error Handling. Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle.
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.
CS203 Java Object Oriented Programming Errors and Exception Handling.
Java Software Solutions Foundations of Program Design Sixth Edition
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 14 Exception Handling and Text.
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
1 Chapter 18 Exception Handling. 2 Motivations F Program runs into a runtime error –program terminates abnormally F How can you handle the runtime error.
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
Chapter 11 Exception Handling F Exceptions and Exception Types F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Exception Handling.
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.
Exceptions. Exception Abnormal event occurring during program execution Examples –Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
Java Software Solutions Lewis and Loftus Chapter 14 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Advanced Flow of Control --
COMP Exception Handling Yi Hong June 10, 2015.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 15 Exceptions and.
Chapter 11: Exception Handling Exceptions and Exception Types Exceptions and Exception Types Claiming Exceptions Claiming Exceptions Throwing Exceptions.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exceptions. Exception  Abnormal event occurring during program execution  Examples Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
1 Chapter 15 Exceptions and Assertions. 2 Objectives F To know what is exception and what is exception handling (§15.2). F To distinguish exception types:
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 15 Exceptions and Assertions Nothing is impossible.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
Chapter 12 Exceptions and File Input/Output
Eighth Lecture Exception Handling in Java
Java Exceptions a quick review….
Chapter 13 Exception Handling
16 Exception Handling.
Chapter 12 Exception Handling And Text IO
Chapter 13 Exception Handling
Exception Handling in Java Reference: COS240 Syllabus
Topic: Exception Handling
Chapter 12 Exception Handling and Text IO
Chapter 7 Exceptions and Assertions
Chapter 12 Exception Handling and Text IO
Chapter 14 Exception Handling and Text IO
EE422C Software Implementation II
Chapter 11—Exceptions Handling Exceptions Throwing Exceptions.
ATS Application Programming: Java Programming
Chapter 12 Exception Handling and Text IO
Chapter 12 Exception Handling
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Chapter 11 Exception Handling and Text I/O
Chapter 12 Exception Handling and Text IO
Chapter 12 Exception Handling and Text IO
Chapter 14 Exception Handling and Text IO
Chapter 13 Exception Handling
Chapter 12: Exceptions and Advanced File I/O
Chapter 12 Exception Handling and Text IO
Tenth step for Learning C++ Programming
Chapter 14 Exception Handling and Text IO
Chapter 12 Exception Handling and Text IO Part 1
Exceptions.
Chapter 12 Exception Handling and Text IO
Chapter 12 Exception Handling and Text IO
Exceptions.
Exception Handling and Event Handling
Exception Handling.
Presentation transcript:

Chapter 13 Exception Handling Exceptions and Exception Types Claiming Exceptions Throwing Exceptions Catching Exceptions Rethrowing Exceptions The finally Clause Cautions When Using Exceptions Creating Your Own Exception Classes (Optional)

Exceptions and Exception Types

Claiming, Throwing, and Catching Exceptions

Claiming Exceptions public void myMethod() throws IOException throws IOException, OtherException

Throwing Exceptions throw new TheException(); TheException e = new TheException(); throw e;

Throwing Exceptions Example public Rational divide(Rational r) throws Exception { if (r.numer == 0) { throw new Exception("divisor cannot be zero"); } long n = numer*r.denom; long d = denom*r.numer; return new Rational(n,d);

Catching Exceptions try { statements; } catch (Exception1 e) { handler for exception1 catch (Exception2 e) { handler for exception2 ... catch (ExceptionN e) { handler for exceptionN

Catching Exceptions

Example 13.1 Claiming, Throwing, and Catching Exceptions Objective: Write a program to test the new Rational class. TestRationalException Rational Run

Example 13.2 Exceptions in GUI Applications An error message appears on the console, but the GUI application continues running. Re-run the MenuDemo applet from Example 11.9 and divide by 0 to see how a GUI deals with unhandled exceptions. MenuDemo Run

Rethrowing Exceptions try { statements; } catch(TheException e) { perform operations before exits; throw e;

The finally Clause try { statements; } catch(TheException e) { handling e; finally { finalStatements;

Cautions When Using Exceptions Exception handling separates error-handling code from normal programming tasks, thus making programs easier to read and to modify. Be aware, however, that exception handling usually requires more time and resources because it requires instantiating a new exception object, rolling back the call stack, and propagating the errors to the calling methods.

Example 13.3 (Optional) Creating Your Own Exception Classes Objective: This program creates a Java applet for handling account transactions. The applet displays the account id and balance, and lets the user deposit to or withdraw from the account. For each transaction, a message is displayed to indicate the status of the transaction: successful or failed. In case of failure, the failure reason is reported.

Example 13.3, cont. InsufficientFundException Account NegativeAmountException AccountApplet Run