230 likes | 256 Views
Learn about error handling, checked and unchecked exceptions, stack trace, and the try-catch mechanism in Java. Understand how to handle exceptions effectively in your code to ensure robustness and prevent unexpected failures.
E N D
Introduction • Users may use our programs in an unexpected ways. • Due to design errors or coding errors, our programs may fail in unexpected ways during execution, or may result in an abnormal program termination • It is our responsibility to produce robust code that does not fail unexpectedly. • Consequently, we must design error handling into our programs.
Errors and Error Handling • Some typical causes of errors: • File system errors (i.e. disk is full, disk has been removed) • Network errors (i.e. network is down, URL does not exist) • Calculation errors (i.e. divide by 0) • More typical causes of errors: • Array errors (i.e. accessing element –1) • Conversion errors (i.e. convert ‘q’ to a number)
Exceptions • What are they? • An exception is a representation of an error condition or a situation that is not the expected result of a method. • Exceptions are built into the Java language and are available to all program code.
Checked Exceptions • How are they used? • Exceptions fall into two categories: • Checked Exceptions • Unchecked Exceptions • Checked exceptions are inherited from the core Java class Exception. They represent compile time exceptions that are your responsibility to check. • Checked exceptions must be handled in your code. Otherwise, the compiler will issue an error message.
Unchecked Exceptions • Unchecked exceptions are runtime exceptions that result at runtime from conditions that you should not have allowed in the first place (inherited from RuntimeException). • You do not have to do anything with an unchecked exception. • But, if not handled, your program will terminate with an appropriate error message.
Definitions • Stack trace • Name of the exception in a descriptive message that indicates the problem • Complete method-call stack • exceptionObject.printStackTrace(); • Exception message: returns a detailed message which describes the exception. • exceptionObject.getMessage();
Exception Handling • Exception handling is accomplished through • the “try – catch” mechanism: handle the exception by yourself, • or by a “throws” clause in the method declaration: pass exception handling “up the chain”. • If the method contains code that may cause a checked exception, you MUST handle the exception OR pass the exception up the chain.
Coding Exceptions • Try-Catch Mechanism • Wherever your code may trigger an exception, the normal code logic is placed inside a block of code starting with the “try” keyword: • After the try block, the code to handle the exception should it arise is placed in a block of code starting with the “catch” keyword. • If none of the code inside the try block throws an exception, then the program skips the catch clause.
Coding Exceptions • Example • try {… normal program code} catch(ArithmeticException AEx){ … }catch(Exception e) {… exception handling code} finally{ … code to close some files or release resources }
finally Block • Consists of finally keyword followed by a block of code enclosed in curly braces • Optional in a try statement • This block contains code that is ALWAYS executed whether or not an exception is thrown in the corresponding try block or any of its corresponding catch blocks. • If present, is placed after the last catch block • Finally blocks can be used for operations that must happen no matter what (i.e. cleanup operations such as closing a file)
Using the throws Clause • Exceptions can be thrown by statements in method’s body or by methods called in method’s body • Exceptions can be of types listed in throws clause or subclasses
Coding Exceptions • Passing the exception • In any method that might throw an exception, you may declare the method as “throws” that exception, and thus avoid handling the exception yourself. • You are not allowed to add more throws specifiers to a subclass method than are present in the superclass method. • Example • public void myMethod throws IOException {… normal code with some I/O … throw new IOException();}