error and exception

Views:
 
     
 

Presentation Description

for all cs student

Comments

Presentation Transcript

Error management and exception handling : 

Error management and exception handling By Hiren Sardhara

INTRODUCTION Rarely does a program run successfully at its very first attempt. Errors are the wrong that can make a program go wrong. It is therefore important to detect and manage properly all the possible error condition in the program so that the program will not terminate or crash during execution. : 

INTRODUCTION Rarely does a program run successfully at its very first attempt. Errors are the wrong that can make a program go wrong. It is therefore important to detect and manage properly all the possible error condition in the program so that the program will not terminate or crash during execution.

Errors : 

Errors Errors are classified into two categories:- Compile-time errors:- All syntax errors will be detected and displayed by the java compiler and therefore these errors are known as compile time errors Run-time errors :- Run time errors are type of errors which are occurred by dividing an integer by zero, trying to cast an instance of a class to one of its sub class.

Errors : 

Errors Error occurred in execution time Abnormal termination of program Wrong execution result Provide an exception handling mechanism in language system Improve the reliability of application program Allow simple program code for exeception check and handling into source

Exception Definition : 

Exception Definition Treat exception as an object All exceptions are instances of a class extended from Throwable class or its subclass. Generally, a programmer makes new exception class to extend the Exception class which is subclass of Throwable class.

Exception Definition : 

Exception Definition class UserErr extends Exception { } class UserClass { UserErr x = new UserErr(); // ... if (val < 1) throw x; }

Definition of Exception : 

Definition of Exception Error Class Critical error which is not acceptable in normal application program Exception Class Possible exception in normal application program execution Possible to handle by programmer

System-Defined Exception : 

System-Defined Exception Raised implicitly by system because of illegal execution of program When cannot continue program execution any more Created by Java System automatically Exception extended from Error class and RuntimeException class [DivByZero.java]

Exception Occurrence : 

Exception Occurrence Raised implicitly by system Raised explicitly by programmer throw Statement [ThrowStatement.java] throw ThrowableObject; Throwable class or its sub class

Exception Occurrence : 

Exception Occurrence class ThrowStatement extends Exception { public static void exp(int ptr) { if (ptr == 0) throw new NullPointerException(); } public static void main(String[] args) { int i = 0; ThrowStatement.exp(i); } } java.lang.NullPointerException at ThrowStatement.exp(ThrowStatement.java:4) at ThrowStatement.main(ThrowStatement.java:8)

Exception Handling : 

Exception Handling try-catch-finally Statement Check and Handle the Exception [ExceptionHandler.java] try { // … } catch (ExceptionType1 identifier) { // … } catch (ExceptionType2 identifier) { // … } finally { // … }

Exception Handling : 

Exception Handling Default Exception Handler When system-defined exception occurred, if programmer does not deal with it, it would be processed by default exception handler Simple function to output error message and exit Execution Order of Exception Handler Finally clause is executed independent of exception and catch [SystemHandler.java] , [FinallyClause.java]

authorStream Live Help