To deal with exceptions                          1
ObjectivesOn completion of this period, you would be able to know:• Exception handling mechanism in Java• How to deal with...
Recap• In the previous class, you have studied about the  features and advantages of exceptions• Advantages are   • Except...
Exception Handling Mechanism In Java• As soon as on exception occurs, the Java runtime  generates an exception object of t...
Exception Handling Mechanism In Java                                                      Contd . . .• Then, the runtime s...
Exception Handling Mechanism In Java                                                Contd . . .• The system continuously c...
What The Java Run Time Does When An              Exception Occurs ?     Contd . . .• The method call sequence is     Metho...
Exception Handling Mechanism In Java                                                                     Contd . . .•   He...
How To Deal With Exceptions• Java exception handling is managed via five  keywords  •   try  •   catch  •   throw  •   thr...
How To Deal With Exceptions Contd...• Program statements that you want to monitor for  exceptions are put within a try blo...
How To Deal With Exceptions         Contd..• System-generated exceptions are automatically  thrown by the Java run-time sy...
How To Deal With Exceptions           Contd..• Any exception that is thrown out of a method must   be specified as such by...
Format Of An Exception-Handling Blocktry {         // block of code to monitor for errors}catch (ExceptionType1 exOb) {   ...
Discussion• How many try blocks can be there in a method?• It depends on the type of exceptions we fore-  see• How many ca...
Summary• In this class we have discussed  • Exception handling mechanism in java  • The keywords necessary for handling   ...
Quiz1. Program statements that you want to monitor for   exceptions are put within a try block   A. True   B. False       ...
Quiz2. Throw keyword is used to throw an exception,   manually   A. True   B. False                                       ...
Frequently Asked Questions1. Explain how to deal with exceptions2. List the keywords that the java uses for handling   exc...
Upcoming SlideShare
Loading in...5
×

Dealing exception.43

189

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
189
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Dealing exception.43

  1. 1. To deal with exceptions 1
  2. 2. ObjectivesOn completion of this period, you would be able to know:• Exception handling mechanism in Java• How to deal with exceptions 2
  3. 3. Recap• In the previous class, you have studied about the features and advantages of exceptions• Advantages are • Exception is a built-in class in Java • Separation of logic of program for error handling section • Classification of exceptions possible 3
  4. 4. Exception Handling Mechanism In Java• As soon as on exception occurs, the Java runtime generates an exception object of the required type• This is known as creating and throwing exception• It then checks the method, where the exception is generated, for any code to handle the exception• This method is known as exception handling code 4
  5. 5. Exception Handling Mechanism In Java Contd . . .• Then, the runtime system checks for each block in that method for catch block• Catch block is known as the exception handler• If there is no catch block • The system checks for catch block, in the calling method of the current method 5
  6. 6. Exception Handling Mechanism In Java Contd . . .• The system continuously checks nesting methods until main method in checked• If the current method is main method, then the exception message is shown on the error output• If there is a catch block in any method, the exception will be handled 6
  7. 7. What The Java Run Time Does When An Exception Occurs ? Contd . . .• The method call sequence is Method3 without an exception handler shown• The call sequence starts from Method2 without an Method call the main method exception handler• main calls Method1 Method1 with an Method call• Method1 calls Method2 exception handler• Method2 calls Method3 Method call main Fig. 43.1 Call stack
  8. 8. Exception Handling Mechanism In Java Contd . . .• Here, in Method3 Throws Method3 without an exception occurs exception exception handler Looking for• Method3 does not appropriate contain the exception handler Forwards Method2 without an handler exception exception handler• Method3 is called by method2 which has no Looking for exception handler Catches Method1 with an appropriate some exception handler handler• Method2 is called by exception method1• Method1 has exception main handler• In Method1 exception is handled Fig. 43.2 Searching the call stack for exception handler
  9. 9. How To Deal With Exceptions• Java exception handling is managed via five keywords • try • catch • throw • throws • finally 9
  10. 10. How To Deal With Exceptions Contd...• Program statements that you want to monitor for exceptions are put within a try block• If an exception occurs within the try block, it is thrown• Your code can catch this exception using catch and handle it in some rational manner 10
  11. 11. How To Deal With Exceptions Contd..• System-generated exceptions are automatically thrown by the Java run-time system• To manually throw an exception, use the keyword throw 11
  12. 12. How To Deal With Exceptions Contd..• Any exception that is thrown out of a method must be specified as such by a throws clause• Any code that absolutely must be executed before a method returns is put in a finally block• finally block is useful – for writing clean up codee.g : To close an opened for file To close database connection To re-initialize variable. etc 12
  13. 13. Format Of An Exception-Handling Blocktry { // block of code to monitor for errors}catch (ExceptionType1 exOb) { // exception handler for ExceptionType1}catch (ExceptionType2 exOb) { // exception handler for ExceptionType2}finally { // block of code to be executed before try block ends} 13
  14. 14. Discussion• How many try blocks can be there in a method?• It depends on the type of exceptions we fore- see• How many catch blocks can be there for a try block?• Any number of catch blocks 14
  15. 15. Summary• In this class we have discussed • Exception handling mechanism in java • The keywords necessary for handling exceptions • try, catch, throw, throws and finally 15
  16. 16. Quiz1. Program statements that you want to monitor for exceptions are put within a try block A. True B. False 16
  17. 17. Quiz2. Throw keyword is used to throw an exception, manually A. True B. False 17
  18. 18. Frequently Asked Questions1. Explain how to deal with exceptions2. List the keywords that the java uses for handling exception mechanism 18
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×