Download ppt - Exceptions in Java

Transcript
Page 1: Exceptions in Java

EXCEPTIONSException is an occurrence that alters the normal program flow.Program terminates when an exception is thrown.To avoid from this, we must handle the exceptions.Try block & Catch clause.

Page 2: Exceptions in Java

Normal format of exception handling

try{

risky code

}catch (Name of Exception){

What to do if an exception is thrown

}

Page 3: Exceptions in Java

Finally Block

Definitely ExecutesTry must be followed by,

1. a catch clause

2. many catch clauses

3. a finally block

4. catch clauses and a finally blockNo code between try and catch

Page 4: Exceptions in Java

Exception Class

Java.lang.ExceptionAll Exceptions are sub classes of

the class ExceptionCreating our own Exception by

extending the class ExceptionThe Keyword throw

Page 5: Exceptions in Java

Exception Hierarchy

Throwable

ExceptionsErrors

RuntimeException

StackoverflawError

AssertionError

InterruptedException

ArithmeticException

Page 6: Exceptions in Java

Errors

Errors represent unusual situations that have not caused by the program.

Normally we cannot recover from them.

So we don’t handle errors.

Page 7: Exceptions in Java

Using multiple catches &Trys

Catches must be in the same order as exception hierarchy (sub class to super class)

Largest catch must be the last.Some exceptions cannot use without a

risk. eg:-IOException,InterruptedException Multiple trys & trys inside try is possible.

Page 8: Exceptions in Java

Checked & Unchecked Exceptions

Some Exceptions are checked in compile time.

RuntimeExceptions and it’s subclasses are unchecked.

Others are checked in compile time.The keyword throws is used to guard

risky methods. It’s not handling &just passing the bulk.

Page 9: Exceptions in Java

Re throwing Exceptions

We can re throw exceptions from the catch clause.

Can give a try-catch from within a catch clause.

Cannot use the same Exception name in re throwing.

Page 10: Exceptions in Java

JVM & Programmatically thrown Exceptions

JVM exceptions – either exclusively or most logically thrown by JVM.

Eg:- nullpointer exceptionProgrammatic – thrown by application

and/or programmer.Eg:- NumberformatException,

IllegalArgumentException


Recommended