Chapter 11—Exceptions Handling Exceptions Throwing Exceptions.

Slides:



Advertisements
Similar presentations
Topics Introduction Types of Errors Exceptions Exception Handling
Advertisements

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.
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.
Errors and Exceptions The objectives of this chapter are: To understand the exception handling mechanism defined in Java To explain the difference between.
© 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.
Exception Handling 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
Chapter 12 By Tony Gaddis Modified by Elizabeth Adams Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
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 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.
COP INTERMEDIATE JAVA Exception Handling Serialization.
Exception Handling.  What are errors?  What does exception handling allow us to do?  Where are exceptions handled?  What does exception handling facilitate?
Exceptions and Exception Handling (1) Carl Alphonce CSE116.
Exceptions and Exception Handling (2) Carl Alphonce CSE116.
Exceptions and Exception Handling (continued) Carl Alphonce CSE116.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
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.
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.
Exception Handling. Exceptions and Errors When a problem encounters and unexpected termination or fault, it is called an exception When we try and divide.
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.
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
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.
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
I NTRODUCTION TO PROGRAMMING Starting Out with Java: From Control Structures through Objects CS 146 Class Notes Fall 10.
Exception. Runtime Error Consider the following program: public class BadArray { public static void main(String[] args) { // Create an array with three.
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.
© Mohamed Nuzrath Java Programming :: Chapter 6 :: Prepared & Presented By :: Mohamed Nuzrath [ Major In Programming ] NCC Programme coordinator IT Lecturer.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 10 Exceptions and Advanced File I/O.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
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.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
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.
Geoff Holmes Week 5 problems Handling Throwing Catching Multiple exceptions Finally clause Examples Exception Handling.
Exceptions in the Java programming language J. W. Rider.
Object Throwable ErrorException RuntimeException.
Lec.11 (Chapter 11) Exception Jiang (Jen) ZHENG July 13 th, 2005.
Chapter 12: Exceptions and Advanced File I/O
Java Exceptions a quick review….
16 Exception Handling.
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA
Java Programming Fifth Edition
Introduction Exception handling Exception Handles errors
Testing and Exceptions
Exception Handling Chapter 9.
Advanced Java Programming
ATS Application Programming: Java Programming
Chapter 11: Exceptions and Advanced File I/O
Topics Introduction to File Input and Output
Chapter 12 Exception Handling
Exception Handling in Java
Exception Handling and Reading / Writing Files
Exception Handling Chapter 9 Edited by JJ.
Chapter 12: Exceptions and Advanced File I/O
Exceptions (part 2) December 3, 2007 ComS 207: Programming I (in Java)
Exceptions 10-May-19.
Topics Introduction to File Input and Output
Exceptions References: Jacquie Barker, Beginning Java Objects; Rick Mercer, Computing Fundamentals With Java; Wirfs-Brock et. al., Martin Fowler, OOPSLA.
Exception Handling and Event Handling
Presentation transcript:

Chapter 11—Exceptions Handling Exceptions Throwing Exceptions

Handling Exceptions An exception is an object that is generated as the result of an error or an unexpected event. Exception are said to have been “thrown.” It is the programmers responsibility to write code that detects and handles exceptions. Unhandled exceptions will crash a program. Example: BadArray.java Java allows you to create exception handlers.

Handling Exceptions An exception handler is a section of code that gracefully responds to exceptions. The process of intercepting and responding to exceptions is called exception handling. The default exception handler deals with unhandled exceptions. The default exception handler prints an error message and crashes the program.

Exception Classes An exception is an object. Exception objects are created from classes in the Java API hierarchy of exception classes. All of the exception classes in the hierarchy are derived from the Throwable class. Error and Exception are derived from the Throwable class.

Exception Classes Classes that are derived from Error: are for exceptions that are thrown when critical errors occur. (i.e.) an internal error in the Java Virtual Machine, or running out of memory. Applications should not try to handle these errors because they are the result of a serious condition. Programmers should handle the exceptions that are instances of classes that are derived from the Exception class.

FileNotFoundException Exception Classes Object Throwable Exception Error RuntimeException IOException FileNotFoundException EOFException …

Handling Exceptions To handle an exception, you use a try statement. { (try block statements...) } catch (ExceptionType ParameterName) (catch block statements...) 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.

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.

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.

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.java

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.

Handling Exceptions Each exception object has a method named getMessage that can be used to retrieve the default error message for the exception. Example: ExceptionMessage.java ParseIntError.java