Topic 07: Exception Handling

35
Topic 07 : Exception Handling DDOOCP

description

Slides

Transcript of Topic 07: Exception Handling

Page 1: Topic 07: Exception Handling

Topic 07 : Exception HandlingDDOOCP

Page 2: Topic 07: Exception Handling

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.

Page 3: Topic 07: Exception Handling

Problems

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

Page 4: Topic 07: Exception Handling

Problems

Page 5: Topic 07: Exception Handling

Problems

Page 6: Topic 07: Exception Handling

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.

Page 7: Topic 07: Exception Handling

Types of Exception

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

1. checked exceptions and

2. unchecked exceptions.

Page 8: Topic 07: Exception Handling

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.

Page 9: Topic 07: Exception Handling

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.

Page 10: Topic 07: Exception Handling

Built-in Exceptions

Page 11: Topic 07: Exception Handling

Checked Exceptions

1. FileNotFoundException

2. IOException

3. ClassNotFoundException

Page 12: Topic 07: Exception Handling

Unchecked Exceptions

1. ArithmeticException

2. IndexOutOfBoundsException

3. ArrayIndexOutOfBoundsException

4. NullPointerException

5. NumberFormatException

6. StringIndexOutOfBoundsException

Page 13: Topic 07: Exception Handling

Exception Handling Techniques

› The try and catch blocks

› Multiple catch blocks

› The finally block

› Nested try blocks

› The throw clause

› The throws clause

Page 14: Topic 07: Exception Handling

The try and catch blocks

Page 15: Topic 07: Exception Handling

The try and catch blocks

Page 16: Topic 07: Exception Handling

Displaying error messages

Page 17: Topic 07: Exception Handling

Multiple catch Blocks

Page 18: Topic 07: Exception Handling

Multiple catch Blocks

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

Demo MultipleCatch

Page 19: Topic 07: Exception Handling

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.

Page 20: Topic 07: Exception Handling

The finally Block

Page 21: Topic 07: Exception Handling

The nested try block

› try block within try block

Page 22: Topic 07: Exception Handling

Exception Example

Page 23: Topic 07: Exception Handling

Same example using function

Page 24: Topic 07: Exception Handling

The throws clause

Page 25: Topic 07: Exception Handling

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.

Page 26: Topic 07: Exception Handling
Page 27: Topic 07: Exception Handling

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.

Page 28: Topic 07: Exception Handling

The throw clause example

Page 29: Topic 07: Exception Handling

Same example using anonymous object

Page 30: Topic 07: Exception Handling

Same example using function

Page 31: Topic 07: Exception Handling

Same example using OOP concept

Page 32: Topic 07: Exception Handling

Using throws and throw clauses

Page 33: Topic 07: Exception Handling

Custom Exception class

Page 34: Topic 07: Exception Handling
Page 35: Topic 07: Exception Handling

References

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

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