Topic 07: Exception Handling

Post on 20-May-2015

684 views 0 download

Tags:

description

Slides

Transcript of Topic 07: Exception Handling

Topic 07 : Exception HandlingDDOOCP

Learning Outcomes

› Understand the importance of Defensive Programming;

› Use the Exception Class to improve the robustness of a Java project;

› Catch system generated Exceptions using the try – catch structure;

› Display system-generated Exception messages;

› Display programmer generated Exception messages;

› Use the finally block;

› Design a testing strategy and associated test data.

Problems

What will happen during compilation?What will happen during runtime?

Problems

Problems

Defensive Programming

› An exception is an exceptional event or error that occur during program execution and interrupt the flow of the program.

› Defensive programming is the tactic of writing code to reduce future, unanticipated problems i.e. exception.

› It can be done by reducing code complexity, validating all user input and data.

› This display of red lines of error messages is called a stack trace. The stack trace lists the classes and methods that the exception passed through before the program was aborted.

Types of Exception

› The exceptions are broadly classified into two categories, namely,

1. checked exceptions and

2. unchecked exceptions.

Checked Exceptions

› Exceptions that must be handled explicitly by the code are called checked exceptions.

› e.g. SQLException, IOException

› In java, java.lang.Exception is a checked exception and all its subclasses will also be checked.

› For checked exceptions, you either have to put a try/catch block around the code or add a "throws" clause to the method, to indicate that the method might throw this type of exception.

Unchecked Exceptions

› Exceptions that do not need to be handled explicitly are called an unchecked exception.

› e.g. ArithmeticException, NumberFormatException.

› In java java.lang.RuntimeException is an unchecked exception and so are all its subclasses.

Built-in Exceptions

Checked Exceptions

1. FileNotFoundException

2. IOException

3. ClassNotFoundException

Unchecked Exceptions

1. ArithmeticException

2. IndexOutOfBoundsException

3. ArrayIndexOutOfBoundsException

4. NullPointerException

5. NumberFormatException

6. StringIndexOutOfBoundsException

Exception Handling Techniques

› The try and catch blocks

› Multiple catch blocks

› The finally block

› Nested try blocks

› The throw clause

› The throws clause

The try and catch blocks

The try and catch blocks

Displaying error messages

Multiple catch Blocks

Multiple catch Blocks

› Write a program that handles ArrayIndexOutOfBoundsException, ArithmeticException, NumberFormatException and Exception.

Demo MultipleCatch

The finally Block

› The code in the finally block is executed regardless of whether an exception is thrown or not.

› You can have only one finally block for an exception-handler.

The finally Block

The nested try block

› try block within try block

Exception Example

Same example using function

The throws clause

The throws clause

› The throws clause is used when we know that a method may cause exceptions, but the method does not handle those exceptions.

› In such a case, a user has to throw those exceptions to the caller of the method by using the throws clause.

› A throws clause is used when a method is declared.

The throw clause

› Till now, we have learned about catching exceptions thrown by JRE.

› In this section, we will learn to throw exceptions explicitly i.e. custom exception.

› We can throw an object of any exception type in Java by using the new operator with throw clause.

The throw clause example

Same example using anonymous object

Same example using function

Same example using OOP concept

Using throws and throw clauses

Custom Exception class

References

› http://chortle.ccsu.edu/java5/Notes/chap81/ch81_5.html

› http://stackoverflow.com/questions/13767124/java-custom-exception-class-usage