Section 3.3 Exceptional Situations. 3.3 Exceptional Situations Exceptional situation Associated with an unusual, sometimes unpredictable event, detectable.

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

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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
© 2004 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 : Exceptions Intermediate Java Programming Summer 2007.
Exception Handling Xiaoliang Wang, Darren Freeman, George Blank.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Objectives In this chapter you will: Learn what an exception is Learn how to handle exceptions within a program See how a try / catch block is used to.
Exceptions and Exception Handling Carl Alphonce CSE116 March 9, 2007.
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.
© The McGraw-Hill Companies, 2006 Chapter 15. © The McGraw-Hill Companies, 2006 Exceptions an exception is an event that occurs during the life of a program.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology/ George Koutsogiannakis 1.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
Jerry Lebowitz. Topics  Provides a facility for a systematic object oriented approach to handling runtime errors ◦ Can also handle runtime errors.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Exceptions Briana B. Morrison CSE 1302C Spring 2010.
File I/O and Exceptions File I/O Exceptions Throwing Exceptions Try statement and catch / finally clauses Checked and unchecked exceptions Throws clause.
CSI 3120, Exception handling, page 1 Exception and Event Handling Credits Robert W. Sebesta, Concepts of Programming Languages, 8 th ed., 2007 Dr. Nathalie.
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
Lecture 28 More on Exceptions COMP1681 / SE15 Introduction to Programming.
Exceptions and Exception Handling (2) Carl Alphonce CSE116.
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.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
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.
Java Software Solutions Foundations of Program Design Sixth Edition
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
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.
Chapter 12: Exception Handling
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
1 CSC241: Object Oriented Programming Lecture No 27.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…
Java Software Solutions Lewis and Loftus Chapter 14 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Advanced Flow of Control --
Chapter 10 Exceptions. Chapter Scope The purpose of exceptions Exception messages The call stack trace The try-catch statement Exception propagation The.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
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.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
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.
1 An Exception is… An unusual, often unpredictable event, detectable by software or hardware, that requires special processing An exception handler is.
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Exceptions Handling Exceptions with try and catch The finally-block The throws.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
Chapter 3 The Stack ADT. Chapter 3: The Stack ADT 3.1 – Stacks 3.2 – Collection Elements 3.3 – Exceptional Situations 3.4 – Formal Specification 3.5 –
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.
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.
Introduction to Exceptions in Java CS201, SW Development Methods.
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.
Chapter 3 The Stack ADT.
Exceptions, Interfaces & Generics
Introduction to Exceptions in Java
ATS Application Programming: Java Programming
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Abdulmotaleb El Saddik University of Ottawa
Java Basics Exception Handling.
Exception Handling.
Presentation transcript:

Section 3.3 Exceptional Situations

3.3 Exceptional Situations Exceptional situation Associated with an unusual, sometimes unpredictable event, detectable by software or hardware, which requires special processing. The event may or may not be erroneous. For example: –a user enters an input value of the wrong type –while reading information from a file, the end of the file is reached –a user presses a control key combination –an illegal mathematical operation occurs, such as divide-by-zero –an impossible operation is requested of an ADT, such as an attempt to pop an empty stack

Exceptions with Java The Java exception mechanism has three major parts: –Defining the exception – usually as a subclass of Java's Exception class –Generating (raising) the exception – by recognizing the exceptional situation and then using Java's throw statement to "announce" that the exception has occurred –Handling the exception – using Java's try – catch statement to discover that an exception has been thrown and then take the appropriate action

Exceptions and ADTs An Example We modify the constructor of our Date class to throw an exception if it is passed an illegal date First, we create our own exception class: public class DateOutOfBoundsException extends Exception { public DateOutOfBoundsException() { super(); } public DateOutOfBoundsException(String message) { super(message); }

Here is an example of a constructor that throws the exception: public Date(int newMonth, int newDay, int newYear) throws DateOutOfBoundsException { if ((newMonth 12)) throw new DateOutOfBoundsException("month " + newMonth + "out of range"); else month = newMonth; day = newDay; if (newYear < MINYEAR) throw new DateOutOfBoundsException("year " + newYear + " is too early"); else year = newYear; }

An example of a program that throws the exception out to interpreter to handle: public class UseDates { public static void main(String[] args) throws DateOutOfBoundsException { Date theDate; // Program prompts user for a date // M is set equal to user’s month // D is set equal to user’s day // Y is set equal to user’s year theDate = new Date(M, D, Y); // Program continues... } The interpreter will stop the program and print an “exception” message, for example Exception in thread "main" DateOutOfBoundsException: year 1051 is too early at Date. (Date.java:18) at UseDates.main(UseDates.java:57)

An example of a program that catches and handles the exception: public class UseDates { public static void main(String[] args) { Date theDate; boolean DateOK = false; while (!DateOK) { // Program prompts user for a date // M is set equal to user’s month // D is set equal to user’s day // Y is set equal to user’s year try { theDate = new Date(M, D, Y); DateOK = true; } catch(DateOutOfBoundsException DateOBExcept) { output.println(DateOBExcept.getMessage()); } // Program continues... }

General guidelines for using exceptions An exception may be handled any place in the software hierarchy—from the place in the program module where it is first detected through the top level of the program. Unhandled built-in exceptions carry the penalty of program termination. Where in an application an exception should be handled is a design decision; however, exceptions should always be handled at a level that knows what the exception means. An exception need not be fatal. For non-fatal exceptions, the thread of execution can continue from various points in the program, but execution should continue from the lowest level that can recover from the exception.

Java RunTimeException class Exceptions of this class are thrown when a standard run-time program error occurs. Examples of run-time errors are division-by-zero and array-index-out-of-bounds. These exceptions can happen in virtually any method or segment of code, so we are not required to explicitly handle these exceptions. These exceptions are classified as unchecked exceptions.

Error Situations and ADTs When dealing with error situations within our ADT methods, we have several options: –Detect and handle the error within the method itself. This is the best approach if the error can be handled internally and if it does not greatly complicate design. –Detect the error within the method, throw an exception related to the error and therefore force the calling method to deal with the exception. If it is not clear how to handle a particular error situation, this approach might be best - throw it out to a level where it can be handled. –Ignore the error situation. Recall the “programming by contract” discussion, related to preconditions, in Chapter 2. With this approach, if the preconditions of a method are not met, the method is not responsible for the consequences.