unit-v - Unit-V 1 Exception Handling Overview Background...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Unit-V
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Exception Handling Overview Background Errors Exceptions
Background image of page 2
3 Program Errors Syntax (compiler) errors Errors in code construction (Syntax errors) Detected during compilation Run-time errors Operations illegal ( ex:- 2 / 0 ) Detected during program execution Treated as exceptions in Java
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Exceptions An exception is an abnormal condition that arises in a code sequence at run time. or An exception is a run-time error. Examples Division by zero Attempts to use an array index out of bounds . Number input in wrong format (float vs. integer,) Unable to write output to file Missing input file
Background image of page 4
5 Exception Handling Performing action in response to exception Examples Exit program (abort) Ignore exception Deal with exception and continue Print error message Request new data Retry action
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Benefits of exception handling It allows us to fix the error. It prevents program from automatically terminating. Separates Error-Handling Code from Regular Code. Conventional programming combines error detection, reporting, handling code with the regular code, leads to confusion.
Background image of page 6
7 Contd. . Propagates Errors Down the Call Stack , which allows more compact and easier-to-understand code. Example : nested method calls method3 { call readFile; } method2 { call method3; } method1 { call method2; } Assume that only method1 is interested in handling errors in readFile Then a conventional program would be like this : method3 { error = call readFile; if (error) return error else proceed; } method2 { error = call method3; if (error) return error else proceed; } method1 { error = call method2; if (error) handle_error else proceed; }
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 Contd. . The java program will be like this : method3 throws exception { call readFile; } method2 throws exception { call method3; } method1 { try { call method2; … } catch (exception) { handle error } … }
Background image of page 8
9 Exception-Handling Fundamentals A Java exception is an object that describes an exceptional condition that has occurred in a piece of code. When an exceptional condition arises, an object representing that exception is created and thrown in the method that caused the error. An exception can be caught to handle it or pass it on. Exceptions can be generated by the Java run-time system, or they can be manually generated by your code.
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10 Exception-Handling Fundamentals Java exception handling is managed by via five keywords: try, catch, throw, throws, and finally. Program statements to monitor are contained within a try block. If an exception occurs within the try block, it is thrown Code within catch block catch the exception and handle it. System generated exceptions are automatically thrown by
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 94

unit-v - Unit-V 1 Exception Handling Overview Background...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online