6
EXCEPTION EXCEPTION An Exception is a condition that An Exception is a condition that is caused by a is caused by a run-time run-time error in error in the program. the program. When the java Interpreter When the java Interpreter encounter an error such as encounter an error such as dividing an integer by zero, it dividing an integer by zero, it creates an exception object and creates an exception object and throws it (Informs the programmer throws it (Informs the programmer that an error has occurred). that an error has occurred). Submitted By: VISHAL SHARMA Batch-I-456 Roll-NO=01014807714

vishal

Embed Size (px)

Citation preview

EXCEPTIONEXCEPTION

An Exception is a condition that is An Exception is a condition that is caused by a caused by a run-timerun-time error in the error in the

program.program.When the java Interpreter encounter an When the java Interpreter encounter an

error such as dividing an integer by error such as dividing an integer by zero, it creates an exception object and zero, it creates an exception object and throws it (Informs the programmer that throws it (Informs the programmer that

an error has occurred).an error has occurred).Submitted By: VISHAL SHARMA

Batch-I-456Roll-NO=01014807714

EXCEPTION HANDLINGEXCEPTION HANDLING• If we want the program to continue with the If we want the program to continue with the

Execution of the remaining code after an error has Execution of the remaining code after an error has occurred, then we should try to Catch the exception occurred, then we should try to Catch the exception object thrown by the error condition and then object thrown by the error condition and then display an appropriate message for taking corrective display an appropriate message for taking corrective actions.This task is known by Exception Handling.actions.This task is known by Exception Handling.

• So, the process of dealing/Handling with the error is So, the process of dealing/Handling with the error is called Exception Handling to protect the crashing of called Exception Handling to protect the crashing of system and abrupt termination of the program.system and abrupt termination of the program.

SYNTAXSYNTAX• …………....• ………………• trytry• {{• statement; //generates an exceptionstatement; //generates an exception• }}• catch(Exception-type e)catch(Exception-type e)• {{• Statement; //processes the exceptionStatement; //processes the exception• }}• ……....• ……....

KEYWORDSKEYWORDS

• try try • catch catch • finally finally

• throwthrow• throwsthrows

Used for Exception Handling

For Raising Exception

TPYESTPYES• ArithmeticExceptionArithmeticException• ArrayIndexOutOfBoundsExceptionArrayIndexOutOfBoundsException• FileNotFoundExceptionFileNotFoundException• IOExceptionIOException• NullPointerExceptionNullPointerException• OutOfMemoryExceptionOutOfMemoryException

USE OF FINALLY STATEMENTUSE OF FINALLY STATEMENT• Java supports another statement known Java supports another statement known

as finally statement that can be used to as finally statement that can be used to handle an exception that is not caught handle an exception that is not caught by any of previous catch statements.by any of previous catch statements.

• Finally block can be used to handle any Finally block can be used to handle any exception generated wiyhin a try block.exception generated wiyhin a try block.

• It may be added immediately after the It may be added immediately after the try block or after the last catch block .try block or after the last catch block .