CMSC 330: Organization of Programming Languages 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

1 Exceptions: An OO Way for Handling Errors Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software.
Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
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.
Exceptions and Exception Handling Carl Alphonce CSE116 March 9, 2007.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
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.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
Exceptions and Exception Handling Carl Alphonce CSE116.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
CSI 3120, Exception handling, page 1 Exception and Event Handling Credits Robert W. Sebesta, Concepts of Programming Languages, 8 th ed., 2007 Dr. Nathalie.
(c) University of Washington11-1 CSC 143 Java More on Exceptions Reading: Ch. 15.
Exceptions and Exception Handling (1) 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 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.
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. 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.
12.1 Exceptions The limitations of traditional methods of exception handling Error conditions are a certainty in programming Programmers make.
Introduction to Java Chapter 11 Error Handling. Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle.
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.
1 Exception Handling Introduction to Exception Handling Exception Handling in PLs –Ada –C++ –Java Sebesta Chapter 14.
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Object Oriented Programming
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.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Slides Credit Umair Javed LUMS Web Application Development.
CMSC 330: Organization of Programming Languages Polymorphism.
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
COMP Exception Handling Yi Hong June 10, 2015.
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.
ICS 313: Programming Language Theory Chapter 14: Exceptions.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Exceptions and Assertions Chapter 15 – CSCI 1302.
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.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
UNDER THE HOOD: THE JAVA VIRTUAL MACHINE II CS2110 Fall 200 Lecture 25 1.
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:
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Introduction to Exceptions in Java CS201, SW Development Methods.
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
Exceptions in the Java programming language J. W. Rider.
Eighth Lecture Exception Handling in Java
Java Exceptions a quick review….
Tirgul 13 Exceptions 1.
Exceptions 10-Nov-18.
Exceptions 10-Nov-18.
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.
Part B – Structured Exception Handling
CSE 143 Java Exceptions 1/18/2019.
Exceptions 25-Apr-19.
Tutorial Exceptions Handling.
Exceptions 22-Apr-19.
Chapter 12 Exception Handling and Text IO Part 1
CSC 143 Java Errors and Exceptions.
Exceptions 10-May-19.
Exceptions 5-Jul-19.
Exception Handling and Event Handling
Exception Handling.
Presentation transcript:

CMSC 330: Organization of Programming Languages Exceptions

CMSC 3302 Preconditions Functions often have requirements on their inputs // Return maximum element in A[i..j] int findMax(int[] A, int i, int j) {... } –A is nonempty –A isn't null –i and j must be nonnegative –i and j must be less than A.length –i < j (maybe)‏ These are called preconditions

CMSC 3303 Dealing with Errors What do you do if a precondition isn’t met? What do you do if something unexpected happens? –Try to open a file that doesn’t exist –Try to write to a full disk

CMSC 3304 Signaling Errors Style 1: Return invalid value // Returns value key maps to, or null if no // such key in map Object get(Object key); –Disadvantages?

CMSC 3305 Signaling Errors (cont’d)‏ Style 2: Return an invalid value and status static int lock_rdev(mdk_rdev_t *rdev) {... if (bdev == NULL)‏ return -ENOMEM;... } // Returns NULL if error and sets global // variable errno FILE *fopen(const char *path, const char *mode);

CMSC 3306 Problems with These Approaches What if all possible return values are valid? –E.g., findMax from earlier slide What about errors in a constructor? What if client forgets to check for error? What if client can’t handle error? –Needs to be dealt with at a higher level Poor modularity- exception handling code becomes scattered throughout program 1996 Ariane 5 failure classic example of this …

CMSC 3307 Ariane 5 failure Design issues: In order to save funds and ensure reliability, and since the French Ariane 4 was a successful rocket, the Inertial Reference System (SRI) from Ariane 4 was reused for the Ariane 5. What happened?: On June 4, 1996 the Ariane 5 launch vehicle failed 39 seconds after liftoff causing the destruction of over $100 million in satellites. Cause of failure: The SRI, which controls the attitude (direction) of the vehicle by sending aiming commands to the rocket nozzle, sent a bad command to the rocket causing the nozzle to move the rocket toward the horizontal. The vehicle tried to switch to the backup SRI, but that failed for the same reason 72 millisec earlier. The vehicle had to then be destroyed.

CMSC 3308 Why Ariane 5 failed SRI tried to convert a floating point number out of range to integer. Therefore it issued an error message (as a 16 bit number). This 16 bit number was interpreted as an integer by the guidance system and caused the nozzle to move accordingly. –The backup SRI performed according to specifications and failed for the same reason. Ada range checking was disabled since the SRI was supposedly processing at 80% load and the extra time needed for range checking was deemed unnecessary since the Ariane 4 software worked well. The ultimate cause of the problem was that the Ariane 5 has a more pronounced angle of attack and can move horizontally sooner after launch. The “bad value” was actually the appropriate horizontal speed of the vehicle.

CMSC 3309 Better approaches: Exceptions in Java On an error condition, we throw an exception At some point up the call chain, the exception is caught and the error is handled Separates normal from error-handling code A form of non-local control-flow –Like goto, but structured

CMSC Throwing an Exception Create a new object of the class Exception, and throw it if (i >= 0 && i < a.length )‏ return a[i]; throw new ArrayIndexOutOfBounds(); Exceptions thrown are part of the return type in Java –When overriding method in superclass, cannot throw any more exceptions than parent’s version

CMSC Method throws declarations A method declares the exceptions it might throw –public void openNext() throws UnknownHostException, EmptyStackException { … } Must declare any exception the method might throw –Unless it is caught in (masked by) the method –Includes exceptions thrown by called methods –Certain kinds of exceptions excluded

CMSC Exception Hierarchy Throwable ErrorException RuntimeException Checked Unchecked

CMSC Unchecked Exceptions Represent defects in the program (bugs)‏ Are subclasses of RuntimeException Common types: –IllegalArgumentException –NullPointerException –IllegalStateException A method is not obliged to establish a policy for the unchecked exceptions thrown by its implementation (and they almost always do not do so)

CMSC Checked Exceptions Represent invalid conditions in areas outside the immediate control of the program (invalid user input, database problems, network outages, absent files) Are subclasses of Exception A method is obliged to establish a policy for all checked exceptions thrown by its implementation (either pass the checked exception further up the stack, or handle it somehow) Note: RuntimeException (unchecked) is itself a subclass of Exception (checked)‏

CMSC Implementing Exceptions in Java JVM knows about exceptions, and has built-in mechanism to handle them public class A { void foo() { try { Object f = null; f.hashCode(); } catch (NullPointerException e) { System.out.println("caught"); }

CMSC First catch with supertype of the exception catches it finally is always executed try { if (i == 0) return; myMethod(a[i]); } catch (ArrayIndexOutOfBounds e) { System.out.println(“a[] out of bounds”); } catch (MyOwnException e) { System.out.println(“Caught my error”); } catch (Exception e) { System.out.println(“Caught” + e.toString()); throw e; } finally { /* stuff to do regardless of whether an exception */ /* was thrown or a return taken */ } Exception Handling

CMSC Implementing Exns in Java Exception table tells JVM what handlers there are for which region of code void foo(); Code: 0: aconst_null 1: astore_1 2: aload_1 3: invokevirtual #2; //hashCode 6: pop 7: goto 19 10: astore_1 11: getstatic #4; //System.out 14: ldc #5; //String caught 16: invokevirtual #6; //println 19: return Exception table: from to target type NullPointerExn

CMSC Unhandled Exceptions Design battle: resumption vs. termination –Resumption: an exception handler can resume computation at the place where the exception was thrown –Termination: throwing an exception terminates execution at the point of the exception Resumption is risky, but so is termination –What do you think?