DOC PREVIEW
UMD CMSC 330 - Exceptions Parameter Passing

This preview shows page 1-2-23-24 out of 24 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1CMSC 330: Organization of Programming LanguagesExceptionsParameter PassingCMSC 330 2Preconditions• Functions often have requirements on their inputs// Return maximum element in A[i..j]int findMax(int[] A, int i, int j) { ... }–Ais nonempty–Aisn't null–iand j must be nonnegative–iand j must be less than A.length–i<j (maybe)• These are called preconditions2CMSC 330 3Dealing 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 diskCMSC 330 4Signaling Errors• Style 1: Return invalid value// Returns value key maps to, or null if no// such key in mapObject get(Object key);– Disadvantages?3CMSC 330 5Signaling Errors (cont’d)• Style 2: Return an invalid value and statusstatic int lock_rdev(mdk_rdev_t *rdev) {...if (bdev == NULL)return -ENOMEM;...}// Returns NULL if error and sets global// variable errnoFILE *fopen(const char *path, const char *mode);CMSC 330 6Problems 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?– No compiler support• 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 …4CMSC 330 7Ariane 5 failureDesign 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 330 8Why 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.5CMSC 330 9Better 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 structuredCMSC 330 10Throwing an Exception• Create a new object of the class Exception, and throw itif (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 version6CMSC 330 11Method 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 excludedCMSC 330 12Exception HierarchyThrowableError ExceptionRuntimeExceptionCheckedUnchecked7CMSC 330 13• Subclasses of RuntimeException and Error are unchecked– Need not be listed in method specifications• Currently used for things like– NullPointerException– IndexOutOfBoundsException– VirtualMachineError• Is this a good design?Unchecked ExceptionsCMSC 330 14•First catch with supertype of the exception catches it• finally is always executedtry { 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 Handling8CMSC 330 15Implementing Exceptions in Java• JVM knows about exceptions, and has built-in mechanism to handle thempublic class A {void foo() {try {Object f = null;f.hashCode();}catch (NullPointerException e) {System.out.println("caught");}}}CMSC 330 16Implementing Exns in Java• Exception table tells JVM what handlers there are for which region of code– Notice that putting this “off to the side” keeps it out of the main code path• (Though less important for an interpreted language)void foo();Code:0: aconst_null1: astore_12: aload_13: invokevirtual #2;//hashCode6: pop7: goto 1910: astore_111: getstatic #4; //System.out14: ldc #5; //String caught16: invokevirtual #6; //println19: returnException table:from to target type0 7 10 NullPointerExn9CMSC 330 17Implementing Exns in C++• 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• C++ settled on termination– What do you think?CMSC 330: Organization of Programming LanguagesParameter Passingand More on Scoping10CMSC 330 19Order of Evaluation• Will OCaml raise a Division_by_zero exception?–No: && and || are short-circuiting in OCaml• e1 && e2 evaluates e1. If false, it returns false. Otherwise, it returns the result of evaluating e2• e1 || e2 evaluates e1. If true, it returns true. Otherwise, it returns the result of evaluating e2letx=0if x != 0 && (y / x) > 100 thenprint_string "OCaml sure is fun"if x == 0 || (y / x) > 100 thenprint_string "OCaml sure is fun"CMSC 330 20Order of Evaluation (cont’d)• C, C++,


View Full Document

UMD CMSC 330 - Exceptions Parameter Passing

Documents in this Course
Exam #1

Exam #1

6 pages

Quiz #1

Quiz #1

2 pages

Midterm 2

Midterm 2

12 pages

Exam #2

Exam #2

7 pages

Ocaml

Ocaml

7 pages

Parsing

Parsing

38 pages

Threads

Threads

12 pages

Ruby

Ruby

7 pages

Quiz #3

Quiz #3

2 pages

Threads

Threads

7 pages

Quiz #4

Quiz #4

2 pages

Exam #2

Exam #2

6 pages

Exam #1

Exam #1

6 pages

Threads

Threads

34 pages

Quiz #4

Quiz #4

2 pages

Threads

Threads

26 pages

Exam #2

Exam #2

9 pages

Exam #2

Exam #2

6 pages

Load more
Download Exceptions Parameter Passing
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Exceptions Parameter Passing and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Exceptions Parameter Passing 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?