Exceptions in the Java programming language J. W. Rider.

Slides:



Advertisements
Similar presentations
Lilian Blot VARIABLE SCOPE EXCEPTIONS FINAL WORD Final Lecture Spring 2014 TPOP 1.
Advertisements

Topics Introduction Types of Errors Exceptions Exception Handling
Exceptions Ensuring program reliability. Program correctness The term program correctness refers to a program’s working as advertised; that is, it produces.
CS102--Object Oriented Programming
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.
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.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 13 In a language without exception handling: When an exception occurs, control goes to the operating.
© 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.
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
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.
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.
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.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
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 8 Exceptions. Topics Errors and Exceptions try-catch throwing Exceptions Exception propagation Assertions.
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.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
1 Exception and Event Handling (Based on:Concepts of Programming Languages, 8 th edition, by Robert W. Sebesta, 2007)
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 Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
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.
1 Exception Handling Introduction to Exception Handling Exception Handling in PLs –Ada –C++ –Java Sebesta Chapter 14.
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.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 9 : Exception Handling King Fahd University of Petroleum & Minerals College of Computer.
Object Oriented Programming
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.
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.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
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.
Unit 4 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
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.
Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
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:
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.
Chapter 10 – Exception Handling
Exceptions 10-Nov-18.
Exception Handling Chapter 9.
Chapter 11—Exceptions Handling Exceptions Throwing Exceptions.
Exception Handling Chapter 9 Edited by JJ.
Java Programming Exceptions CSC 444 By Ralph B. Bisland, Jr.
Exceptions 10-May-19.
Exception Handling and Event Handling
Presentation transcript:

Exceptions in the Java programming language J. W. Rider

Background Whenever you invoke an object’s method, you have no way of knowing if the “normal” behavior is going to be successfully completed. When building robust programs, you need to accommodate situations outside of the norm.

Methodic communications Ways a method can communicate with other parts of a program Return value Output parameters (object/array) In-scope variable assignment (“side effect”) Method invocation Throw exception

Before exceptions… Testing of return results or global variables after every statement. Global functions would be called to handle all possible conditions. Recovery would be determined in one place in the program. This was fine. However, testing every statement obscured the exception-free nominal path to programmers. Global exception handlers precluded local handling of exception conditions.

Java Exceptions The Throwable class hierarchy The throw statement The throws clause The try statement

Throwable hierarchy Object Throwable Error Exception RuntimeException For serious conditions that a reasonable program should not catch. For conditions that a reasonable program should catch.

Throwable class Constructed with no argument or with a String error message Useful instance methods: –getMessage() –printStackTrace() –toString() Subclasses Error and RuntimeException not required to be listed in throws clause.

Throwable classes Throwable –Error –Exception RuntimeException –ArithmeticException,ArrayStoreException –IllegalArgumentException,IndexOutOfBoundsException –NullPointerException IOException –FileNotFoundException –EndOfFileException

Throw statement throw Throwable-reference ; –throw new MyException(); Interrupts nominal program flow. No more statements in the current sequence are executed. The exception must be handled in an encompassing try statement, or the program terminates.

Jump statements Jump statements interrupt structured program flow. –Return –Break –Continue – Throw Jump statements must be the last in any given sequence of statements. They are frequently part of a conditional statement.

Throws clause Added to a method header to indicate that an unhandled exception may arise during execution of the method. … throws Throwable1, Throwable2, ThrowableN A method that invokes the throwing method must either handle the exception when it arises, or also include a throws clause in its declaration.

Method declaration Method header –Ordinary member –Constructor –Static block/initializer Method body –Enclosed in braces –Abbreviated with a semicolon.

Method header Qualifiers Return-type Method-name Formal arguments Throws clause Signature Prototype

Try statement Provides a structured approach to exception handling. –Try block –Catch section One or more catch blocks –Finally block The try block is always required. Either a catch section or finally block or both follow.

Structured statements Sequence –Block (enclosed in braces) Conditional statements –If statement –Switch statement Iterative statements –While statement –For statement –Do statement Try statement “Canonical” structures Structured exception handling

Try block Identifies the “nominal” flow of control through a try statement. It is a sequence of statements. If no exception occurs, all the statements in the try block are executed in sequence. If an exception occurs, the statement being executed is interrupted, and none of the statements following are executed. The whole try block is interrupted.

Try block figure try { doFirst(); doSecond(); doThird(); } This is our nominal control flow. We establish the try block around those statements where we do NOT want to continue if an exception occurs.

Catch section Immediately follows the try block. Composed of a series of catch blocks. Is only entered if an exception occurs in the try block. Only one of the catch blocks are executed.

Catch block When a catch block is executed, the exception is “deemed” handled, even if the catch body is empty. catch (MyException e) { … // exception handling code }

Finally block Envelopes a sequence of statements that must be executed whether the try block is interrupted or not. NOT Does NOT handle the exception. Does not have any way to determine whether an exception has occurred, or if it has been handled. If an exception has not been handled, the exception is still active at the end of the finally block and interrupts the method where the try statement is embedded.

Example scenario A resource holds items that need to be processed individually. While exceptions may occur at any point during processing, we are only expected to recover from the “EmptyResourceException” that arises when we attempt to extract an item from an empty resource. There is no way to determine if a resource is empty without attempting an extraction. Regardless of whether an exception occurs or not, if we open a resource for processing, we must close the resource.

Example code Resource rez=Resource.open(resourcename); try { while(true) { Item item=rez.extractItem(); item.process(); } catch(EmptyResourceException e) {} finally { rez.close(); }

Exceptions vs Events Exceptions Are part of the Java language. Compiler treats Throwable, Error and RuntimeException special. Program terminated if not handled. Events Are implemented as user classes in a library. No special treatment. Events are notifications that something has happened, but do not require handling.

Summary Exception handling provides a new control flow structure through Java programs. The try statement provides for –watching a nominal sequence of statements –handling the exceptions we expect –ensuring statements that must be executed, are.