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.

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

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.
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.
COMP 121 Week 5: Exceptions and Exception Handling.
Chapter 9 Exception Handling. Chapter Goals To learn how to throw exceptions To be able to design your own exception classes To understand the difference.
Exceptions and Exception Handling Carl Alphonce CSE116 March 9, 2007.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
Java Exceptions. Exceptions Often in computing, operations cannot properly execute because some sort of error has occurred. Some examples: Often in computing,
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.
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
When you use an input or output file that does not exist, what will happen? −The compiler insists that we tell it what the program should do in such case.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism.
Exception Handling.  What are errors?  What does exception handling allow us to do?  Where are exceptions handled?  What does exception handling facilitate?
Lecture 28 More on Exceptions COMP1681 / SE15 Introduction to Programming.
Exceptions and Exception Handling (2) Carl Alphonce CSE116.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exceptions Handling.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
Chapter 8 Exceptions. Topics Errors and Exceptions try-catch throwing Exceptions Exception propagation Assertions.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
06 - Exceptions. 2 ©S. Uchitel, 2004 A familiar sight? Bluescreen.scr.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Chapter 8  Errors and Exceptions Throwable class.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
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
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
Preventing and Correcting Errors
Exception Handling. Exceptions and Errors When a problem encounters and unexpected termination or fault, it is called an exception When we try and divide.
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.
Slides Credit Umair Javed LUMS Web Application Development.
Handling Exceptions in java. Exception handling blocks try { body-code } catch (exception-classname variable-name) { handler-code }
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
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.
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.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
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.
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.
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
CS 61B Data Structures and Programming Methodology July 7, 2008 David Sun.
The Problem Unexpected erroneous situations are often discovered by code which is NOT prepared to remedy the error………. For example.. String in = JOptionPane.showInputDialog(“enter.
Exceptions in the Java programming language J. W. Rider.
Java Exceptions a quick review….
Introduction Exception handling Exception Handles errors
CSE 501N Fall ’09 17: Exception Handling
Chapter 15 – Exception Handling
E x c e p t i o n s Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. — Martin Golding.
Advanced Java Programming
ATS Application Programming: Java Programming
Exception Handling.
Web Design & Development Lecture 7
Chapter 12 Exception Handling and Text IO Part 1
Java Basics Exception Handling.
Exception Handling.
Presentation transcript:

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 code I.e. If your code knows what to do, it should do it. If not, throw an exception.

Example Integer.parseInt method takes a String and tries to interpret it as an int. –If it can, it returns an int value. –If it can’t, it throws an exception. Why is the exception thrown? For two reasons: –there is no int value it can return (through normal control flow) to signal a problem with the input –Integer.parseInt has no idea what the caller wants to do. The method does not know the context of the request.

Example (continued) Possible calling contexts A user-input routine that calls Integer.parseInt with a String that a user entered can, if an exception is thrown, prompt the user for new input. A non-UI routine that calls Integer.parseInt may be reading input from a file, and may take the exception as an indication that there is an error in the input file format, or perhaps that input processing must transition to another state.

Throwing exceptions syntax –throw new Exception() –throw new Exception(“A message”) exception classes –Exception is a subclass of Throwable –RuntimeException is a subclass of Exception any exception which is of this type is unchecked any exception which is NOT of this type is checked

unchecked exceptions “unchecked exceptions are generally the programmer’s fault” [H, pg. 19] the compiler doesn’t force you to deal with them in your program – they just shouldn’t occur in the first place e.g. NullPointerException

checked exceptions “In general, a checked exception is caused by an external condition beyond the programmer’s control.” [H, pg. 19] Your code must either –provide a handler for every checked exception, or –indicate that the exception is propagated further by declaring the exception as thrown by the method in its header

the ‘throws’ clause Examples from [h, pg. 20] public void read(String filename) throws FileNotFoundException { FileReader reader = new FileReader(filename); … } public void read(String filename) throws IOException, ClassNotFoundException { … }

catching exceptions Wrap the code from which you are prepared to handle exceptions in a throw block Provide one or more catch blocks, one for each type of exception your code will handle –catch blocks are attempted in the order given, and the first matching one is used, so list catch block from most specific first to most general last

finally block you may provide a finally block a finally block is always executed –if an exception is not thrown after all the code in the try block has finished normally –if an exception is thrown and is handled by a catch block after the exception handler finishes –if an exception is thrown but is not handled, before the uncaught exception is propagated further.

Example try { … } catch (ExceptionType1 e) { … } catch (ExceptionType2 e) { … } … catch (ExceptionTypeN e) { … } finally { … }

Defining your own exception class Subclass an appropriate existing exception class Provide no-argument and single-argument (String) constructors

The Error class The class Throwable has two subclasses: –Exception –Error What’s the difference –A program can typically recover from an exception and continue processing –A program can typically not recover from an error