Understanding Exception Handling in Programming

exception handling exception handling n.w
1 / 22
Embed
Share

Learn how exception handling works in programming to handle errors gracefully and continue program execution smoothly. Explore the advantages, mechanism, and keywords of exception handling with examples and best practices.

  • Exception Handling
  • Error Handling
  • Programming
  • Learn
  • Advantages

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Exception Handling Exception Handling

  2. CONTENTS Try Throw Catch Throwing an Exception Catching an Exception.

  3. Exception Indication of problem that occurs during a program s execution, example, an attempt to divide by zero, new operator fails to allocate memory for an object. Exception handling provides the standard mechanism for processing errors. It allows the programmer to continue executing as if no problem had been encountered.

  4. Exception Handling Advantages It helps the programmer to write robust and fault-tolerant programs that can deal with problems continue executing or terminate gracefully. Exception handling also is useful for processing problems that occur when a program interacts with software elements, such as member functions, constructors, destructors and classes.

  5. Exception Handling Mechanism 1. 2. 3. 4. Find the problem (Hit the exception). Inform that error has occurred (throw the exception). Receive the error information (catch the exception). Take corrective actions (handle the exception).

  6. Cont Error handling code basically consists of two parts. Detect error and throw the exception (in try block) Catch exception and take appropriate action. (in catch block)

  7. Cont.. Exception handling basically has 3 keywords: 1. Try 2. Throw 3. Catch

  8. Cont.. In try block, we add those block of statements which may generate exceptions. When an exception is detected, it is thrown using a throw statement in a try block. A catch block defined by keyword catch catches the exception thrown by throw statement in the try block and handles it appropriately.

  9. Cont.. The catch block that catches an exception must immediately follows the try block that throws the exception.

  10. Throw and Catch The throw statement is almost similar to function call. The only difference is that instead of calling the function, it calls the catch block. In this sense, the catch block is like function definition with a parameter that matches the type of value being thrown.

  11. Throw The throw expression accepts one parameter as its argument and this is passed to the exception handler. You can have a number of throw statements at different parts of your try block with different values being thrown so that the exception handler on receiving the parameter will know what restorative actions to take.

  12. Catch The exception handler can be identified by the keyword catch . catch always takes only one parameter. The type of the catch parameter is important as the type of the argument passed by the throw expression is checked against it and the catch function with the correct parameter type is executed. This way we can chain multiple exception handlers and only the one with the correct parameter type gets executed.

  13. Cont.. try { // code here } catch (int param) { cout << "int exception"; } catch (char param) { cout << "char exception"; } catch (...) { cout << "default exception"; } try { } // code if ( x ) throw 10; // code if (y) throw 20; //code

  14. Throwing an exception in a Function

  15. Throwing an Exception in a Function

  16. Cont..

  17. Re-throwing an Exception It s possible that an exception handler, upon receiving an exception, might decide either that it cannot process that exception or that it can process the exception only partially. In such cases, the exception handler can defer the exception handling (or perhaps a portion of it) to another exception handler.

  18. Cont.. In either case, you achieve this by re throwing the exception via the statement throw; Regardless of whether a handler can process an exception, the handler can re throw the exception for further processing outside the handler.

  19. Cont.. 1. #include <iostream> 2. #include <exception> 3. using namespace std; 4. void func1() 5. { 6. // throw exception and catch it immediately 7. try 8. { 9. cout << "\n This function throws an exception\n"; 10. throw exception(); //generate exception 11. } // end try

  20. Cont.. 12. catch(exception &) 13. { 14. 15. 16. 17. } // end catch 18. cout << "This also should not print\n"; 19. } // end of func1 cout << " exception is catched in 1st catch block" << "\n Function func1 re throws exception"; throw; //rethrow exception for further processing

  21. Cont.. 20. int main() 21. { 22. try 23. { 24. cout << "\n main invokes function func1\n"; 25. func1(); 26. cout << "This should not print\n"; 27. } // end try 28. catch (exception e) // handle exception 29. { 30. cout << "\n\n Exception handled in main\n"; 31. } // end catch 32. cout << " Program control continues after catch in main\n"; 33. } // end main

  22. Cont..

More Related Content