exception handling

16

Click here to load reader

Upload: muthukumaran-subramanian

Post on 26-May-2015

42 views

Category:

Education


2 download

DESCRIPTION

introduc

TRANSCRIPT

Page 1: Exception handling

Exception Handling

Page 2: Exception handling

What is exception ?

● An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.

● Anything that occurs in an unexpected way is called as exception

Page 3: Exception handling

Error

● These are not exceptions at all, but problems that arise beyond the control of the user or the programmer.

● Errors are typically ignored in your code because you can rarely do anything about an error.

Page 4: Exception handling

Difference between Error and Exception

● An Error "indicates serious problems that a reasonable application should not try to catch.”

● An Exception "indicates conditions that a reasonable application might want to catch."

Page 5: Exception handling

Why exception occurs

An exception can occur for many different reasons, including the following:

● A user has entered invalid data.● A file that needs to be opened cannot be

found.● A network connection has been lost in the

middle of communications or the JVM has run out of memory.

Page 6: Exception handling

Types of Exceptions

● Checked exceptions● Runtime exceptions

Page 7: Exception handling

Checked Exception

● A checked exception is an exception that is typically a user error or a problem that cannot be foreseen by the programmer.

● For example, if a file is to be opened, but the file cannot be found, an exception occurs. These exceptions cannot simply be ignored at the time of compilation.

Page 8: Exception handling

Runtime Exception

● A runtime exception is an exception that occurs that probably could have been avoided by the programmer.

● As opposed to checked exceptions, runtime exceptions are ignored at the time of compilation.

Page 9: Exception handling

Categories of exception

● 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.

Page 10: Exception handling

Advantages of exception handling

● Separating Error-Handling Code from "Regular" Code

● Grouping and Differentiating Error Types

Page 11: Exception handling

Keywords to handle exceptions

Exception Handling contains 5 keywords● 1. try● 2. catch● 3. finally● 4. throws● 5. throw

Page 12: Exception handling

Syntax for Exception handling

try

{}

catch(Exception e){

}

Finally{

}

Page 13: Exception handling

Purpose of try, catch and finally

● The try block identifies a block of code in which an exception can occur.

● The catch block identifies a block of code, known as an exception handler, that can handle a particular type of exception.

● The finally block identifies a block of code that is guaranteed to execute, and is the right place to close files, recover resources, and otherwise clean up after the code enclosed in the try block.

Page 14: Exception handling

Exception Hierarchy

Page 15: Exception handling

Example of Exception handling

Page 16: Exception handling

Sample output