17
EXCEPTIONS -By SWAPNIL S. MHADLEKAR 3-Dec-15 1

Introduction to Exception

Embed Size (px)

Citation preview

Page 1: Introduction to Exception

EXCEPTIONS

-By SWAPNIL S. MHADLEKAR

3-Dec-15 1

Page 2: Introduction to Exception

WHAT IS AN EXCEPTION???

EXCEPTION IS AN ABNORMAL CONDITION WHICH INTERRUPTS THE SMOOTH EXECUTION OF PROGRAM.

E.g.

1)If you are trying to access 6th element in the array:

a[1,2,3,4,5]

a[5]=9;// throw ArrayIndexOutOfBound Exception

2) trying to divide a number by zero

int a =5, b=0;

int result= a/0; //throw Arithmetic Exception

Page 3: Introduction to Exception

EXCEPTION vs ERRORS

Errors are thrown by the runtime system which indicate some conditions during the program execution.

When the error is thrown by the system, application cannot recover it and the program execution halts.

Examples.

Java.lang.OutOfMemoryError

Java.lang.StackOverflowError

Page 4: Introduction to Exception

EXCEPTIONS HIERARCHY

java.lang.Objectjava.lang.Throwable

Page 5: Introduction to Exception

CHECKED vs UNCHECKED EXCEPTIONS

Checked Exception-

Any exception that extends java.lang.Exceptionclass is called a “checked exception”.

A checked exception must be handled using try and catch blocks or the method should declare them in throws.

Page 6: Introduction to Exception

Unchecked Exception

Any exception that extends java.lang.RunTimeException is called “Unchecked Exception”.

They are also called “RunTime Exceptions”.

There is no need to handle the unchecked exception and also to declare using throws.

Page 7: Introduction to Exception

RULES TO FOLLOW:

A try block must be followed by a catch or finally or both.

There should not be any statements between try and catch and finally.

Finally is optional and there should be only one finally block.

There can be multiple catch blocks.

The narrow exceptions must be declared first and the broader exception last.

Page 8: Introduction to Exception

EXCEPTION HANDLING

Exceptions are recoverable. So we need to handle them.

We handle the exception just to ensure that the program is not terminated.

Keywords-

try

catch

throws

throw

finally

Page 9: Introduction to Exception

So How To Deal With Exception??

1) The Method can contain the code for the handler itself:

Page 10: Introduction to Exception

2) The Method can alert other methods about the exception it throws. (Duck)

Page 11: Introduction to Exception

Syntax:

try{//risky code goes hereUsing whatsApp during lecture}

catch(Exception ex){//Recovery code goes hereApologize to teacher}

finally{// part of code which must execute independent of the

exceptionLecture over}

Page 12: Introduction to Exception

throw And throws:

When we say that an exception is thrown, it means that an object of Exception class is created.

It is created by the method where the exception occurred.

This object is then handed over to the Runtime system.

The Runtime system then looks for the code that can handle it.

This code is called “”Exception Handler.

Page 13: Introduction to Exception

Exception Handler:

Page 14: Introduction to Exception

MY CUSTOM EXCEPTION

We can create our own custom exception by extending java.lang.Exception class.

E.g.

public class MyCustomException

extends Exception{

}

Or

public class MyCustomException

extends RunTimeException{

}

Page 15: Introduction to Exception

EXCEPTION PROPAGATION

Method implementation takes place in stack.

Page 16: Introduction to Exception

Propagation:

Page 17: Introduction to Exception

3-Dec-15 17

THANK YOU !!