Exception. Runtime Error Consider the following program: public class BadArray { public static void main(String[] args) { // Create an array with three.

Slides:



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

1 Exceptions: An OO Way for Handling Errors Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software.
Topics Introduction Types of Errors Exceptions Exception Handling
Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Yoshi
Exception Handling. Background In a perfect world, users would never enter data in the wrong form, files they choose to open would always exist, and code.
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
Introduction to Exceptions in Java. 2 Runtime Errors What are syntax errors? What are runtime errors? Java differentiates between runtime errors and exceptions.
© 2012 Pearson Education, Inc. All rights reserved. Chapter 12: Exceptions and Advanced File I/O Starting Out with Java: From Control Structures through.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
Starting Out With Java 5 (Early Objects) Chapter 10 By Tony Gaddis Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
Exception Handling1. 2 Exceptions  Definition  Exception types  Exception Hierarchy  Catching exceptions  Throwing exceptions  Defining exceptions.
MIT-AITI Lecture 14: Exceptions Handling Errors with Exceptions Kenya 2005.
Chapter 12 By Tony Gaddis Modified by Elizabeth Adams Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
For use of Cleveland State's IST410 Students only 1 Exception.
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.
Java Exceptions. Types of exceptions  Checked exceptions: A checked exception is an exception that is typically a user error or a problem that cannot.
Exception Handling Topics We will discuss the following main topics: – Handling Exceptions – Throwing Exceptions – More about Input/Output Streams.
12-2 Chapter Topics Chapter 12 discusses the following main topics: Part I: Exceptions Handling Exceptions Throwing Exceptions Part II: More about Input/Output.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Exceptions and Exception Handling (2) Carl Alphonce CSE116.
Exceptions and Exception Handling (continued) Carl Alphonce CSE116.
Exception handling Dealing with life’s little surprises.
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.
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
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.
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
Exception Handling. Exceptions and Errors When a problem encounters and unexpected termination or fault, it is called an exception When we try and divide.
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
Exceptions and Advanced File I/O
Exceptions. Exception Abnormal event occurring during program execution Examples –Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
I NTRODUCTION TO PROGRAMMING Starting Out with Java: From Control Structures through Objects CS 146 Class Notes Fall 10.
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.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 10 Exceptions and Advanced File I/O.
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.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
Exceptions Handling Prepared by: Ligemm Mae del Castillo.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
CSE 1341 File I/O Exception Handling. Introduction to File Processing Data stored in variables and arrays is temporary. Main Memory Main Memory Secondary.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
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.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
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 Advanced File I/O. 2 Contents I. Handling Exceptions II. Throwing Exceptions III. Advanced Topics 1. Binary Files 2. Random.
Chapter 12: Exceptions and Advanced File I/O
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA
Chapter 10 – Exception Handling
Introduction to Exceptions in Java
Exceptions and Advanced File I/O
Testing and Exceptions
Chapter 11—Exceptions Handling Exceptions Throwing Exceptions.
Chapter 11: Exceptions and Advanced File I/O
Chapter 12 Exception Handling
Handling Exceptions.
Chapter 12: Exceptions and Advanced File I/O
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
Presentation transcript:

Exception

Runtime Error Consider the following program: public class BadArray { public static void main(String[] args) { // Create an array with three elements. int[] numbers = { 1, 2, 3 }; // Attempt to read beyond the bounds // of the array. for (int index = 0; index <= 3; index++) System.out.println(numbers[index]); }

10-3 Handling Exceptions To handle an exception, you use a try statement. try { (try block statements...) } catch (ExceptionType ParameterName) { (catch block statements...) } finally {… } First the keyword try indicates a block of code will be attempted (the curly braces are required). This block of code is known as a try block.

10-4 Handling Exceptions A try block is: – one or more statements that are executed, and – can potentially throw an exception. The application will not halt if the try block throws an exception. After the try block, a catch clause appears.

10-5 Handling Exceptions A catch clause begins with the key word catch : catch (ExceptionType ParameterName) – ExceptionType is the name of an exception class and – ParameterName is a variable name which will reference the exception object if the code in the try block throws an exception. The code that immediately follows the catch clause is known as a catch block (the curly braces are required). The code in the catch block is executed if the try block throws an exception.

10-6 Handling Exceptions This code is designed to handle a FileNotFoundException if it is thrown. try { File file = new File ("MyFile.txt"); Scanner inputFile = new Scanner(file); } catch (FileNotFoundException e) { System.out.println("File not found."); } The Java Virtual Machine searches for a catch clause that can deal with the exception. Example: OpenFile.javaOpenFile.java

10-7 Handling Exceptions The parameter must be of a type that is compatible with the thrown exception’s type. After an exception, the program will continue execution at the point just past the catch block.

10-8 Retrieving the Default Error Message Each exception object has a method named getMessage that can be used to retrieve the default error message for the exception. Example: – ExceptionMessage.java ExceptionMessage.java

10-9 Polymorphic References To Exceptions When handling exceptions, you can use a polymorphic reference as a parameter in the catch clause. Most exceptions are derived from the Exception class. A catch clause that uses a parameter variable of the Exception type is capable of catching any exception that is derived from the Exception class.

10-10 Polymorphic References To Exceptions try { number = Integer.parseInt(str); } catch (Exception e) { System.out.println("The following error occurred: " + e.getMessage()); } The Integer class’s parseInt method throws a NumberFormatException object. The NumberFormatException class is derived from the Exception class.

10-11 Handling Multiple Exceptions The code in the try block may be capable of throwing more than one type of exception. A catch clause needs to be written for each type of exception that could potentially be thrown. The JVM will run the first compatible catch clause found. The catch clauses must be listed from most specific to most general. Example: SalesReport.javaSalesReport.java

10-12 Exception Classes Object Throwable ExceptionError RuntimeExceptionIOException FileNotFoundExceptionEOFException … … … … More specific

10-13 Exception Handlers There can be many polymorphic catch clauses. A try statement may have only one catch clause for each specific type of exception. try { number = Integer.parseInt(str); } catch (NumberFormatException e) { System.out.println("Bad number format."); } catch (NumberFormatException e) // ERROR!!! { System.out.println(str + " is not a number."); }

10-14 Exception Handlers The NumberFormatException class is derived from the IllegalArgumentException class. try { number = Integer.parseInt(str); } catch (IllegalArgumentException e) { System.out.println("Bad number format."); } catch (NumberFormatException e) // ERROR!!! { System.out.println(str + " is not a number."); }