Chapter 11: Exception Handling Exceptions and Exception Types Exceptions and Exception Types Claiming Exceptions Claiming Exceptions Throwing Exceptions.

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

Errors and Exceptions The objectives of this chapter are: To understand the exception handling mechanism defined in Java To explain the difference between.
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.
Chapter 12 By Tony Gaddis Modified by Elizabeth Adams Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Exception Handling Topics We will discuss the following main topics: – Handling Exceptions – Throwing Exceptions – More about Input/Output Streams.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
1 Exception Handling (in a nutshell). 2 Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle the.
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.
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.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 14 Exception Handling and Text.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 12 Exception Handling and Text.
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.
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
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.
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“);
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.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Exception Handling and Text.
Odds and Ends. CS 21a 09/18/05 L14: Odds & Ends Slide 2 Copyright © 2005, by the authors of these slides, and Ateneo de Manila University. All rights.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
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.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
CS 112 Programming 2 Lecture 08 Exception Handling & Text I/O (1)
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.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
EXCEPTIONS. Catching exceptions Whenever a runtime error occurs, it create an exception object. The program stops running at this point and Python prints.
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
Exception Handling. You learned that there are three categories of errors: syntax errors, runtime errors, and logic errors. Syntax errors arise because.
Chapter 12 Exceptions and File Input/Output
Chapter 13 Exception Handling
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
CNS 3260 C# .NET Software Development
Chapter 11—Exceptions Handling Exceptions Throwing Exceptions.
ATS Application Programming: Java Programming
Chapter 12 Exception Handling
Chapter 13 Exception Handling
Chapter 11 Exception Handling and Text I/O
Chapter 12 Exception Handling and Text IO
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
Chapter 14 Exception Handling and Text IO
Chapter 12 Exception Handling and Text IO Part 1
CSC 143 Java Errors and Exceptions.
Exceptions.
Chapter 12 Exception Handling and Text IO
Chapter 12 Exception Handling and Text IO
Exceptions.
Presentation transcript:

Chapter 11: Exception Handling Exceptions and Exception Types Exceptions and Exception Types Claiming Exceptions Claiming Exceptions Throwing Exceptions Throwing Exceptions Catching Exceptions Catching Exceptions Creating Your Own Exception Classes Creating Your Own Exception Classes Rethrowing Exceptions Rethrowing Exceptions The finally Clause The finally Clause Cautions When Using Exceptions Cautions When Using Exceptions

Exceptions and Exception Types

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

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

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

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

Example 11.2 Catching Exceptions Objective: Write a program to test the new Rational class. Objective: Write a program to test the new Rational class. TestRationalException Run Rational

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

Example 11.4 Creating Your Own Exception Classes F Objective: Create 10 accounts and transfer funds among the accounts. Raise exceptions if a transaction amount is negative or the account balance is less than the transaction amount. TestMyExceptionRun NegativeAmountExceptionAccount InsufficientFundException

Example 11.5 Using Exceptions in Applets Objective: An applet for account transactions that displays account ID and balance and performs deposits and withdrawals. For each transaction, a message indicates success or failure of the transaction, and reports any reason for failure. Objective: An applet for account transactions that displays account ID and balance and performs deposits and withdrawals. For each transaction, a message indicates success or failure of the transaction, and reports any reason for failure. AccountApplet Run Applet Viewer

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

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

Cautions When Using Exceptions Example 11.6: Demonstrating Performance Differences With and Without Exception Handling Example 11.6: Demonstrating Performance Differences With and Without Exception Handling UsingNoException TestMyExceptionWithTiming Run