Exceptions The Need for Exceptions Throwing Exceptions

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

Introduction to Exceptions in Java. 2 Runtime Errors What are syntax errors? What are runtime errors? Java differentiates between runtime errors and exceptions.
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,
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
Exception Handling. Introduction An exception is an abnormal condition that arises in a code sequence at run time. In computer languages that do not support.
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
Objectives Understanding what an exception is Understanding the heirarchy of exception classes Learn the types of exception and how to catch and handle.
CPSC150 Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 CPSC150 Exceptions When things.
1 From Yesterday private = accessible only to the class that declares it public = accessible to any class at all protected = accessible to the class and.
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 CIS 304 Intermediate Java Programming for Business.
06 - Exceptions. 2 ©S. Uchitel, 2004 A familiar sight? Bluescreen.scr.
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.
Preventing and Correcting Errors
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
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.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
Java Programming Exception Handling. The exception handling is one of the powerful mechanism provided in java. It provides the mechanism to handle the.
Slides Credit Umair Javed LUMS Web Application Development.
COMP Exception Handling Yi Hong June 10, 2015.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
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/
LECTURE 8: EXCEPTIONS CSC 212 – Data Structures. Error Handling Goals  What should we do when an error occurs?  Should alert system to the error  May.
Question of the Day completes starts  What word completes the 1 st word & starts the 2 nd one: DON???CAR.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
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.
Agenda Introduction Errors and Exception Exception Hierarchy Classification of Exceptions Built in Exceptions Exception Handling in Java User defined.
Exception Handling. You learned that there are three categories of errors: syntax errors, runtime errors, and logic errors. Syntax errors arise because.
Object Throwable ErrorException RuntimeException.
Exceptions In this lecture:
Chapter 10 – Exception Handling
Tirgul 13 Exceptions 1.
MIT AITI 2003 Lecture14 Exceptions
Introduction to Exceptions in Java
Introduction to Exceptions in Java
Exceptions 10-Nov-18.
Exceptions 10-Nov-18.
Handling Exceptions.
EE422C Software Implementation II
Advanced Java Programming
ATS Application Programming: Java Programming
Exceptions & exception handling
Exception Handling in Java
CMSC 202 Exceptions 2nd Lecture.
CMSC 202 Exceptions 2nd Lecture.
Web Design & Development Lecture 7
Chapter 13 Exception Handling
Intro to Exceptions (c) Eraj Basnayake
CMSC 202 Exceptions 2nd Lecture.
Exceptions 25-Apr-19.
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.
Java Programming Exceptions CSC 444 By Ralph B. Bisland, Jr.
Exceptions 22-Apr-19.
Chapter 12 Exception Handling and Text IO Part 1
SWE 619 Last modified Fall 2007 Saket Kaushik, Paul Ammann
Exception Handling Contents
Exceptions 10-May-19.
Exceptions References: Jacquie Barker, Beginning Java Objects; Rick Mercer, Computing Fundamentals With Java; Wirfs-Brock et. al., Martin Fowler, OOPSLA.
Java Basics Exception Handling.
Exception Objects An exception is an abnormal condition that arises in a code sequence at rum time. Exception is a way of signaling serious problem.
Exceptions 5-Jul-19.
CMSC 202 Exceptions 2nd Lecture.
Exception Handling.
Presentation transcript:

Exceptions The Need for Exceptions Throwing Exceptions Exception Objects Object Construction Failure Catching Exceptions The Error Exception The Finally Clause OOP with Java, David J. Barnes Exceptions

The Need For Exceptions ''The World Outside'' is potentially hostile. A public interface allows external access. Possible exceptional conditions: Trying to move a ship that is moored. Trying to book a seat in a full theatre. Going faster than the local speed limit. The exceptional condition is based on the Ship's context, rather than its state. OOP with Java, David J. Barnes Exceptions

Possible Responses Print an error message. Terminate the program. Silently ignore the request. Implement the request anyway. Implement a modified or faked action. Request a correction interactively. Return the wrong answer. Return an error indication. OOP with Java, David J. Barnes Exceptions

Throwing an Exception Java's standard 'error indication' is an exception. Each is represented by an Exception object. ArithmeticException. ArrayIndexOutOfBoundsException. ClassCastException. NumberFormatException. OOP with Java, David J. Barnes Exceptions

The Throw Statement class UncheckedMain2 { public static void main(String[] args){ // Terminate the whole program. throw new RuntimeException( "I just can't cope!"); } Exception in thread "main" java.lang.RuntimeException: I just can't cope! at java.lang.Throwable.<init>(Throwable.java:78) at java.lang.Exception.<init>(Exception.java:42) at java.lang.RuntimeException.<init>(RuntimeException.java:47) at UncheckedMain2.main(UncheckedMain2.java:4) OOP with Java, David J. Barnes Exceptions

Exception Objects Exception objects have methods. public String getMessage(); public void printStackTrace(); public String toString(); These methods are used when an exception is caught, to provide diagnostic information. OOP with Java, David J. Barnes Exceptions

The Throws Clause public void setSpeed(double s) throws RuntimeException { // Check the validity of the request. if(Math.abs(s) > maximumSpeed){ // Too fast. Don't proceed. throw new RuntimeException("Speed "+s+ " is beyond the maximum of "+ maximumSpeed); } else{ speed = s; OOP with Java, David J. Barnes Exceptions

Object Construction Failure class Circle { public Circle(double r) throws RuntimeException { if(r <= 0){ throw new RuntimeException( "The radius must be positive: "+r); } radius = r; ... OOP with Java, David J. Barnes Exceptions

Checked Exceptions RuntimeException is an unchecked exception. Unchecked do not require a throws clause. Unchecked do not have to be 'caught'. Exception is a checked exception. Checked do require a throws clause. Checked do have to be dealt with in some way. OOP with Java, David J. Barnes Exceptions

Catching Exceptions An exception that is not caught or handled will result in immediate program failure. Catching an exception means we might be able to fix the problem. Try a slower speed for the ship. Java's try statement provides an exception handling (catching) mechanism. OOP with Java, David J. Barnes Exceptions

The Try Statement try{ // Things could go wrong within this block. ... } catch(Exception e){ // Something went wrong. finally{ OOP with Java, David J. Barnes Exceptions

Catching an Exception try{ // Construction could fail. Ship argo = new Ship(); double speed = ...; ... // Setting a new speed could fail. argo.setSpeed(speed); } catch(RuntimeException e){ System.err.println(e.getMessage()); OOP with Java, David J. Barnes Exceptions

Anticipating Multiple Exceptions try{ // The protected statements. ... } catch(IndexOutOfBoundsException e){ catch(ArithmeticException e){ catch(RuntimeException e){ OOP with Java, David J. Barnes Exceptions

Propagating Exceptions The rules for checked and unchecked exceptions differ. Unchecked exceptions. Propagated to the caller if there is no local caller. Checked exceptions. Must be handled locally, or propagated via an explicit throws clause. OOP with Java, David J. Barnes Exceptions

Rethrowing an Exception public void method() throws Exception { try{ // Protected statements ... } catch(Exception e){ // Partial recovery. // Rethrow the exception. throw e; OOP with Java, David J. Barnes Exceptions

The Error Exception The java.lang package defines Error as representing a particularly serious exception. One from which recovery is impossible. For instance, an out-of-memory problem. Use sparingly, and as a last resort. Anticipate exceptions, and plan for their recovery. OOP with Java, David J. Barnes Exceptions

The Finally Clause public void method(){ try{ // T2 might throw an exception. Statement-T1; Statement-T2; Statement-T3; } catch(Exception e){ Statement-C; finally{ Statement-F; OOP with Java, David J. Barnes Exceptions

Often Used to Close Files FileReader inFile = null; FileWriter outFile = null; try{ inFile = new FileReader(source); outFile = new FileWriter(destination,true); ... } finally{ if(inFile != null){ inFile.close(); if(outFile != null){ outFile.close(); OOP with Java, David J. Barnes Exceptions

Review An object might be asked to perform inappropriate actions. An object often does not know how to respond in such situations. Exceptions allow an object to leave the caller to sort out the problem. An exception is either checked or unchecked. OOP with Java, David J. Barnes Exceptions

Review (cont.) Exceptions may be caught and handled, or propagated. Uncaught (unhandled) exceptions result in premature program termination. The finally clause provides an always-executed route out of a try statement. Either the catch clause or finally clause may be omitted. OOP with Java, David J. Barnes Exceptions