java exception handling

37
1

Upload: pendekanti-surendra-kumar

Post on 24-Mar-2016

242 views

Category:

Documents


0 download

DESCRIPTION

Java Exception Handling

TRANSCRIPT

Page 1: Java Exception Handling

1

Page 2: Java Exception Handling

2

Page 3: Java Exception Handling

3

Page 4: Java Exception Handling

The Call Stack – By the call stack is meant the sequence of method calls from the current method and back to the Main method of the program. If a method A calls B, and B calls C then the call stack looks like this:

A

B

C

Propagating Exceptions

You don't have to catch exceptions thrown from other methods. If you cannot do anything about the exception where the method throwing it is called, you can just let the method propagate the exception up the call stack to the method that called this method. If you do so the method calling the method that throws the exception must also declare to throw the exception.

Reference - http://tutorials.jenkov.com/java-exception-handling/basic-try-catch-finally.html

4

Page 5: Java Exception Handling

5

Page 6: Java Exception Handling

6

Page 7: Java Exception Handling

Catch or Propagate Exceptions?

You might be wondering whether you should catch or propate exceptions thrown in your program. It depends on the situation. In many applications you can't really do much about the exception but tell the user that the requested action failed. In these applications you can usually catch all or most exceptions centrally in one of the first methods in the call stack. You may still have to deal with the exception while propagating it though (using finally clauses). For instance, if an error occurs in the database connection in a web application, you may still have to close the database connection in a finally clause, even if you can't do anything else than tell the user that the action failed. How you end up handling exceptions also depends on whether you choose checked or unchecked exceptions for your application.

7

Page 8: Java Exception Handling

8

Page 9: Java Exception Handling

9

Page 10: Java Exception Handling

Multiple Catch Blocks

it is possible to have several catch blocks for the same try-block. This is usually the case if the code inside the try-block throws more than one type of exception. But, multiple catch blocks can also be used in the case where all the exceptions thrown inside the try-block are the same type or subclasses of that type

Try {

//call some methods that throw IOException's

} catch (FileNotFoundException e) {

} catch (IOException e) {

}

10

Page 11: Java Exception Handling

In java it is possible to define two catergories of Exceptions and Errors.

JVM Exceptions: - These are exceptions/errors that are exclusively or logically thrown by the JVM. Examples : NullPointerException, ArrayIndexOutOfBoundsException, ClassCastException,

Programmatic exceptions . These exceptions are thrown explicitly by the application or the API programmers Examples: IllegalArgumentException, IllegalStateException.

11

Page 12: Java Exception Handling

12

Page 13: Java Exception Handling

13

Page 14: Java Exception Handling

Sun VM specification: “In the Java programming language, throwing an exception results in an immediate nonlocal transfer of control from the point where the exception was thrown. This transfer of control may abruptly complete, one by one, multiple statements, constructor invocations, static and field initializer evaluations, and method invocations. The process continues until a catch clause is found that handles the thrown value. If no such clause can be found, the current thread exits”.

“Exceptions are expensive and should be used exceptionally. Do not use exception handling constructs if it can be avoided”

Example:

How to handle null pointer scenario:

Use:

public void addEmployee(Employee employee) {

if( employee != null ) {

// insert into the persistent layer

}

}

Instead of :

public void addEmployee(Employee employee) {

try {

// insert into the persistent layer

} catch(NullPointerException ex) {

// log the exceptions14

Page 15: Java Exception Handling

15

Page 16: Java Exception Handling

16

Page 17: Java Exception Handling

17

Page 18: Java Exception Handling

18

Page 19: Java Exception Handling

19

Page 20: Java Exception Handling

Exception wrapping is wrapping is when you catch an exception, wrap it in a different exception and throw that exception. Here is an example:

try {

dao.readPerson();

} catch (SQLException sqlException) {

throw new MyException("error text", sqlException);

}

The method dao.readPerson() can throw an SQLException. If it does, the SQLException is caught and wrapped in a MyException. Notice how the SQLException (the sqlException variable) is passed to the MyException's constructor as the last parameter.

Why Use Exception Wrapping?

The main reason one would use exception wrapping is to prevent the code further up the call stack from having to know about every possible exception in the system.

Reference - http://tutorials.jenkov.com/java-exception-handling/exception-wrapping.html

20

Page 21: Java Exception Handling

21

Page 22: Java Exception Handling

22

Page 23: Java Exception Handling

23

Page 24: Java Exception Handling

Exceptions usually occur in methods of low-level layer that should not try to fix the problem.These methods should propagate the exceptions upwards, while propagating the exceptions should be wrapped into more generic user-defined exceptions. The reason for doing this is simply decoupling the two-layers.

Example:

In Data Access Layer if RDBMS is used for persistence SQLException might occur, and if Files are used IOException might occur. These exceptions should be suppressed in that layer. If handled the caller will never realize the abnormality. At the same time the caller should be abstracted about the mechanism used for persistence, hence wrap that exception into user defined exception class like PersistenceException and propagate to the caller layer.

24

Page 25: Java Exception Handling

25

Page 26: Java Exception Handling

26

Page 27: Java Exception Handling

27

Page 28: Java Exception Handling

28

Page 29: Java Exception Handling

29

Page 30: Java Exception Handling

30

Page 31: Java Exception Handling

31

Page 32: Java Exception Handling

32

Page 33: Java Exception Handling

33

Page 34: Java Exception Handling

34

Page 35: Java Exception Handling

35

Page 36: Java Exception Handling

36

Page 37: Java Exception Handling

37