(c) University of Washington11-1 CSC 143 Java More on Exceptions Reading: Ch. 15.

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.
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
CSM-Java Programming-I Spring,2005 Exceptions Lesson - 7.
Errors and Exceptions The objectives of this chapter are: To understand the exception handling mechanism defined in Java To explain the difference between.
COMP 121 Week 5: Exceptions and Exception Handling.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
Exceptions and Exception Handling Carl Alphonce CSE116 March 9, 2007.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
MIT-AITI Lecture 14: Exceptions Handling Errors with Exceptions Kenya 2005.
For use of Cleveland State's IST410 Students only 1 Exception.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
Java Exceptions. Types of exceptions  Checked exceptions: A checked exception is an exception that is typically a user error or a problem that cannot.
11-Jun-15 Exceptions. 2 Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
1 From Yesterday private = accessible only to the class that declares it public = accessible to any class at all protected = accessible to the class and.
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.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
Java Exceptions. Intro to Exceptions  What are exceptions? –Events that occur during the execution of a program that interrupt the normal flow of control.
06 - Exceptions. 2 ©S. Uchitel, 2004 A familiar sight? Bluescreen.scr.
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 Exceptions. Intro to Exceptions  What are exceptions? –Events that occur during the execution of a program that interrupt the normal flow of control.
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.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
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.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 9 : Exception Handling King Fahd University of Petroleum & Minerals College of Computer.
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
CIS 270—Application Development II Chapter 13—Exception Handling.
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.
Slides Credit Umair Javed LUMS Web Application Development.
Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.
Class Design: Handling Errors Reading: 2 nd Ed: Chapter 15 3 rd Ed: Chapter 11 Exercises 2 nd Ed: P15.5, P15.6 (Hint: look at documentation for Scanner.
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.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Exceptions and Assertions Chapter 15 – CSCI 1302.
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.
EXCEPTIONS There's an exception to every rule.. 2 Introduction: Methods  The signature of a method includes  access control modifier  return type 
MIT AITI 2004 – Lecture 14 Exceptions Handling Errors with Exceptions.
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.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
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.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Exceptions in the Java programming language J. W. Rider.
Java Exceptions a quick review….
MIT AITI 2003 Lecture14 Exceptions
Introduction Exception handling Exception Handles errors
CS102 – Exceptions David Davenport Latest: May 2015
CSE 501N Fall ’09 17: Exception Handling
Java Programming Language
CSE 143 Java Exceptions 1/18/2019.
Chapter 12: Exceptions and Advanced File I/O
CSC 143 Java More on Exceptions.
CSC 143 Java Errors and Exceptions.
Exception Handling and Event Handling
Exception Handling.
Presentation transcript:

(c) University of Washington11-1 CSC 143 Java More on Exceptions Reading: Ch. 15

(c) University of Washington11-2 Review Can throw exceptions to indicate unusual or erroneous conditions Create an object to hold data about the kind of exception Can be an instance of any subclass of Throwable Throw it Can catch exceptions of desired kinds Exceptions help to separate the error-handling code from the regular code

(c) University of Washington11-3 Generic Exceptions So far, only throw exceptions of standard exception classes [such as??] private void updateBalance( double amount) { if (this.balance + amount < 0) { throw new IllegalArgumentException("insufficient funds"); } else { this.balance = this.balance + amount; } To catch this error, must catch all IllegalArgumentExceptions... any drawbacks???

(c) University of Washington11-4 Custom Exceptions A better way would be to create a special kind of exception, just for the error condition that's being reported private void updateBalance( double amount) { if (this.balance + amount < 0) { throw new InsufficientFundsException( ); } else { this.balance = this.balance + amount; } Think of two reasons why that's good...

(c) University of Washington11-5 Custom Exception Classes To create a custom exception: public class InsufficientFundsException extends RuntimeException { public InsufficientFundsException( ) { // invoke RuntimeException's constructor to set the error message super("insufficient funds"); } Exception subclass is a regular class may have instance variables, constructors, methods, etc. etc.

(c) University of Washington11-6 Throwable/Exception Hierarchy Throwable ErrorException RuntimeException ArithmeticException NullPointerException IllegalArgumentException InsufficientFundsException...

(c) University of Washington11-7 Handling Custom Exceptions As expected: try { mySavingsAccount.withdraw(100.00); myCheckingAccount.deposit(100.00); } catch (InsufficientFundsException exn) { System.out.println("Transaction failed: " + exn.getMessage()); } Now won't accidentally catch unrelated IllegalArgumentExceptions

(c) University of Washington11-8 Declaring Thrown Exceptions A method can declare the exceptions that it might throw, either directly or indirectly public void withdraw(double amount) throws InsufficientFundsException { this.updateBalance(- amount); } private void updateBalance(double amount) throws InsufficientFundsException { if (this.balance + amount < 0) { throw new InsufficientFundsException( ); } else { this.balance = this.balance + amount; }

(c) University of Washington11-9 Throws Clauses Syntax ( ) throws, …, For regular methods, abstract methods, methods in interfaces, and constructors Informs callers what exceptions they might need to handle It's good practice for a method to either handle all exceptions that may be raised by the messages it sends, or document using throws clauses that some exceptions might be propagated out of it to its callers

(c) University of Washington11-10 throw vs. throws Two separate keywords in Java Make sure you know the difference! Hint: where can you use each of them??

(c) University of Washington11-11 Checked vs. Unchecked Exceptions For exception classes that are subclasses of RuntimeException, throws clauses are optional The idea is that these exceptions might be so frequently thrown, that it would be pointless to list them everywhere Called unchecked exceptions But for other exceptions, e.g. subclasses of Exception other than RuntimeException, throws clauses are required Called checked exceptions

(c) University of Washington11-12 Throwable/Exception Hierarchy Throwable ErrorException RuntimeException ArithmeticException NullPointerException IllegalArgumentException InsufficientFundsException [checked] IOException [checked] FileNotFoundException

(c) University of Washington11-13 Custom Checked Exceptions Java requires: all checked exceptions be handled, or be listed in the throws clause Good practice (according to some programmers): custom exceptions should be checked exceptions public class InsufficientFundsException extends Exception {... } public void withdraw( double amount) throws InsufficientFundsException {…} private void updateBalance( double amount) throws InsufficientFundsException { … } public void transferTo(BankAccount other, double amount) // no throws clause needed, since all possible checked exceptions handled { try { this.withdraw(amount); other.deposit(amount); } catch (InsufficientFundsException exn) { … } }

(c) University of Washington11-14 Throws Clauses and Overriding When one method overrides another old rule: it must have the same signature (argument and result types) new rule: it must have the same signature and it must include the “same” exceptions in its throws clause (“same” since derived types are also OK) One consequence: when declaring a general interface, e.g. for all Shapes, each method in the interface must mention all exceptions that any overriding method implementation might throw This can be tricky to get right