DOC PREVIEW
UMD CMSC 132 - Exceptions in Java

This preview shows page 1-2-3-4-5 out of 15 pages.

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

Unformatted text preview:

Exceptions in Java Fawzi Emad Chau Wen Tseng Department of Computer Science University of Maryland College Park Overview Background Errors Exceptions Java support Representing exceptions Generating handling exceptions Designing using exceptions 1 Program Errors Syntax compiler errors Errors in code construction grammar types Detected during compilation Run time errors Operations illegal impossible to execute Detected during program execution Treated as exceptions in Java Logic errors Operations leading to incorrect program state May or may not lead to run time errors Detect by debugging code Exceptions Rare event outside normal behavior of code Examples Division by zero Access past end of array Out of memory Number input in wrong format float vs integer Unable to write output to file Missing input file 2 Exception Handling Performing action in response to exception Examples Exit program abort Ignore exception Deal with exception and continue Print error message Request new data Retry action Exception Handling Problem May not be able to handle exception locally Not enough information in method class Need more information to decide action Handle exception in calling function s instead Decide at application level instead of library Examples Incorrect data format ask user to reenter data ask user for new filename Insufficient disk space ask user to delete files Unable to open file Will need to propagate exception to caller s 3 Exception Handling Exit Program Approach Exit program with error message error code Example if error System err println Error found System exit 1 message error code Problem Drastic solution Event must be handled by user invoking program Program may be able to deal with some exceptions Exception Handling Error Code Approach Exit function with return value error code Example A if error return 1 B if retval A 1 return 1 Problems Calling function must check process error code May forget to handle error code May need to return error code to caller Agreement needed on meaning of error code Error handling code mixed with normal code 4 Exception Handling Throw Exception Approach Throw exception Example A if error throw new ExceptionType Java exception backtracks to B caller s until matching catch try block found A catch ExceptionType e action Exception Handling Throw Exception Advantages Compiler ensures exceptions are caught eventually No need to explicitly propagate exception to caller Backtrack to caller s automatically Class hierarchy defines meaning of exceptions No need for separate definition of error codes Exception handling code separate clearly marked 5 Representing Exceptions Exceptions represented as Objects derived from class Throwable Code public class Throwable extends Object Throwable No error message Throwable String mesg Error message String getMessage Return error mesg void printStackTrace Record methods called location Representing Exceptions Java Exception class hierarchy Two types of exceptions checked unchecked 6 Representing Exceptions Java Exception class hierarchy ClassNotFoundException ClassNotFoundException Exception Exception CloneNotSupportedException CloneNotSupportedException IOException IOException ArithmeticException ArithmeticException AWTException AWTException NullPointerException NullPointerException RuntimeException RuntimeException Object Object IndexOutOfBoundsException IndexOutOfBoundsException Throwable Throwable NoSuchElementException NoSuchElementException LinkageError LinkageError VirtualMachoneError VirtualMachoneError Error Error AWTError AWTError Checked Checked Unchecked Unchecked Exceptions Examples FileNotFoundException java io Request to open file fails IllegalArgumentException java lang Method passed illegal inappropriate argument IOException java io Generic I O error NullPointerException java lang Attempt to access object using null reference UnsupportedOperationException java lang Object does not provide requested operation 7 Unchecked Exceptions Class Error RunTimeException Serious errors not handled by typical program Usually indicate logic errors Example NullPointerException IndexOutOfBoundsException Catching unchecked exceptions is optional Handled by Java Virtual Machine if not caught Checked Exceptions Class Exception except RunTimeException Errors typical program should handle Used for operations prone to error Example IOException ClassNotFoundException Compiler requires catch or declare Catch and handle exception in method OR Declare method can throw exception force calling function to catch or declare exception in turn Example void A throws ExceptionType 8 Generating Handling Exceptions Java primitives Try Throw Catch Finally Procedure for using exceptions 1 Enclose code generating exceptions in try block 2 Use throw to actually generate exception 3 Use catch to specify exception handlers 4 Use finally to specify actions after exception Java Syntax try throw new eType1 catch eType1 e action catch eType2 e action finally action try block encloses throws throw jumps to catch catch block 1 run if type match catch block 2 run if type match final block always executes 9 Java Primitive Try Forms try block Encloses all statements that may throw exception Scope of try block is dynamic Includes code executed by methods invoked in try block and their descendents Java Primitive Try Example try A B try block encloses all exceptions in A B exceptions may be caught internally in A B or propagated back to caller s try block void A throws Exception declares exception B void B throws Exception declares exception throw new Exception propagate to caller 10 Java Primitive Throw Indicates exception occurred Normally specifies one operand Object of class Exception When an exception is thrown 1 Control exits the try block 2 Proceeds to closest matching exception handler after the try block 3 Execute code in exception handler 4 Execute code in final block if present Java Primitive Catch Placed after try block Specifies code to be executed for exception Code in catch block exception handler Catch block specifies matching exception type Can use multiple catch blocks for single try To process different types of exceptions First matching catch block executed Superclass may subsume catch for subclass If catch block for superclass occurs first 11 Java Primitive Catch Example class eType1 extends Exception try throw new eType1 catch Exception e Catch block 1 action matches all exceptions catch eType1 e Catch block 2 action matches


View Full Document

UMD CMSC 132 - Exceptions in Java

Documents in this Course
Notes

Notes

8 pages

Recursion

Recursion

12 pages

Sorting

Sorting

31 pages

HTML

HTML

7 pages

Trees

Trees

19 pages

HTML

HTML

18 pages

Trees

Trees

19 pages

Honors

Honors

19 pages

Lecture 1

Lecture 1

11 pages

Quiz #3

Quiz #3

2 pages

Hashing

Hashing

21 pages

Load more
Loading Unlocking...
Login

Join to view Exceptions in Java 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 in Java 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?