1 / 50

Exception Handling Basics in C++

Learn the basics of exception handling in C++, including defining exception classes, multiple throws and catches, and exception specifications.

dgunter
Download Presentation

Exception Handling Basics in C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Concordia UniversityDepartment of Computer Science and Software EngineeringCOMP345 – Advanced Program Design with C++Lecture 9: Exception Handling

  2. Learning Objectives • Exception Handling Basics • Defining exception classes • Multiple throws and catches • Exception specifications • Programming Techniques for Exception Handling • When to throw exceptions • Exception class hierarchies

  3. Introduction • Typical approach to development: • Write programs assuming things go as planned • Get "core" working • Then take care of "exceptional" cases • C++ exception-handling facilities • Handle "exceptional" situations • Mechanism "signals" unusual happening • Another place in code "deals" with exception

  4. Exception-Handling Basics • Meant to be used "sparingly" • In "involved" situations • Difficult to teach such large examples • Approach: • Simple "toy" examples, that would notnormally use exception-handling • Keep in mind "big picture"

  5. Introduction to Exception Handling • Sometimes the best outcome can be when nothing unusual happens • However, the case where exceptional things happen must also be prepared for • C++ exception handling facilities are used when the invocation of a method may cause something exceptional to occur

  6. Introduction to Exception Handling • C++ library software (or programmer-defined code) provides a mechanism that signals when something unusual happens • This is called throwing an exception • In another place in the program, the programmer must provide code that deals with the exceptional case • This is called handling the exception

  7. try-throw-catch Mechanism • The basic way of handling exceptions in C++ consists of the try-throw-catch trio • The try block contains the code for the basic algorithm • It tells what to do when everything goes smoothly • It is called a try block because it "tries" to execute the case where all goes as planned • It can also contain code that throws an exception if something unusual happens try { CodeThatMayThrowAnException }

  8. try-throw-catch Mechanism throw new ExceptionClassName(PossiblySomeArguments); Or throw “object” • When an exception is thrown, the execution of the surrounding try block is stopped • Normally, the flow of control is transferred to another portion of code known as the catch block • The value thrown is the argument to the throwoperator, and is always an object (possibly an instance of some exception class) • The execution of a throw statement is called throwing an exception

  9. try-throw-catch Mechanism • A throw statement is similar to a method call: thrownew ExceptionClassName(SomeString); • In the above example, the object of class ExceptionClassName is created using a string as its argument • This object, which is an argument to the throw operator, is the exception object thrown • Instead of calling a method, a throw statement calls a catch block

  10. try-throw-catch Mechanism • When an exception is thrown, the catch block begins execution • The catch block has one parameter • The exception object thrown is plugged in for the catch block parameter • The execution of the catch block is called catching the exception, or handling the exception • Whenever an exception is thrown, it should ultimately be handled (or caught) by some catch block

  11. try-throw-catch Mechanism catch(Exception e) { ExceptionHandlingCode } • A catch block looks like a method definition that has a parameter • It is not really a method definition, however • A catch block is a separate piece of code that is executed when a program encounters and executes a throw statement in the preceding try block • A catch block is often referred to as an exception handler • It can have at most one parameter

  12. try-throw-catch Mechanism catch(Exceptione) { . . . } • The identifier e in the above catch block heading is called the catch block parameter • The catch block parameter does two things: • It specifies the type of thrown exception object that the catch block can catch (e.g., an Exception class object above) • It provides a name (for the thrown object that is caught) on which it can operate in the catch block • Note: The identifier e is often used by convention, but any non-keyword identifier can be used

  13. try-throw-catch Mechanism • When a try block is executed, three things can happen: 1. No exception is thrown in the try block • The code in the try block is executed to the end of the block • The catch block is skipped • The execution continues with the code placed after the catch block

  14. try-throw-catch Mechanism 2. An exception is thrown in the try block and caught in the catch block • The rest of the code in the try block is skipped • Control is transferred to a following catch block • The thrown object is plugged in for the catch block parameter • The code in the catch block is executed • The code that follows that catch block is executed (if any)

  15. try-throw-catch Mechanism 3. An exception is thrown in the try block and there is no corresponding catch block to handle the exception • The rest of the code in the try block is skipped • The function throws the exception to its calling function. • The calling function either catches the exception using a catch block, or itself throws the exception. • If all functions fail to catch the exception, the exception will eventually be thrown all the way to the main function. If the main function cannot catch the exception, the program ends, itself throwing the exception.

  16. Toy Example • Imagine: people rarely run out of milk: cout << "Enter number of donuts:";cin >> donuts;cout << "Enter number of glasses of milk:";cin >> milkdpg = donuts/static_cast<double>(milk);cout << donuts << "donuts.\n"; << milk << "glasses of milk.\n"; << "You have " << dpg << "donuts for each glass of milk.\n"; • Basic code assumes never run out of milk

  17. Toy Example if-else • Notice: If no milkdivide by zero error! • Program should accommodate unlikelysituation of running out of milk • Can use simple if-else structure: if (milk <= 0) cout << "Go buy some milk!\n";else{…} • Notice: no exception-handling here

  18. Toy Example with Exception Handling: Display 18.2 Same Thing Using Exception Handling

  19. Toy Example Discussion • Code between keywords try and catch • Same code from ordinary version, exceptif statement simpler:if (milk <= 0) throw donuts; • Much cleaner code • If "no milk"  do something exceptional • The "something exceptional" is providedafter keyword catch

  20. Toy Example try-catch • Try block • Handles "normal" situation • Throws an exception on an “exceptional” situation • Catch block • Handles "exceptional" situations • Provides separation of normal from exceptional • Not big deal for this simple example, butimportant concept

  21. try block • Basic method of exception-handling istry-throw-catch • Try block:try{ Some_Statements <some code with a throws statement or a function invocation that might throw an exception> Some_More_Statements} • Contains code for basic algorithm when allgoes smoothly • A try block must be followed by at least one catch block

  22. throw • Inside try-block, when something unusual happens:try{ Code_To_Try if (exceptional_happened) throw object; More_Code} • Keyword throw followed by an object id • Called "throwing an exception“ • An exception can be thrown outside of a try block. • Upon throwing the exception, the function will halt and throw the exception to the calling function without even trying to catch it.

  23. catch-block • When something thrown  goes somewhere • In C++, flow of control goes from try-block tocatch-block • try-block is "exited" and control passes to catch-block • Executing catch block called "catching theexception" • Exceptions must be "handled" in somecatch block • There cannot be a catch block without a preceding try block.

  24. catch-block More • Recall:catch(int e){ cout << e << " donuts, and no milk!\n"; << " Go buy some milk.\n";} • Looks like function definition with int parameter! • Not a function, but works similarly • Throw like "function call"

  25. catch-block Parameter • Recall: catch(int e) • "e" called catch-block parameter • Each catch block can have at most ONEcatch-block parameter • Does two things: • type name specifies what kind of thrownvalue the catch-block can catch • Provides name for thrown value caught;can "do things" with value

  26. Defining Exception Classes • throw statement can throw value of any type • Exception class • Contains objects with information to be thrown • Can have different types identifying each possible exceptional situation • Still just a class • An "exception class" due to how it’s used

  27. Exception Class for Toy Example • Consider:class NoMilk{public: NoMilk() { } NoMilk(int howMany) : count(howMany) { } int getcount() const { return count; }private: int count;}; • throw NoMilk(donuts); • Invokes constructor of NoMilk class

  28. Inheriting from exceptionClass #include <iostream> #include <exception> using namespace std; class myexception: public exception{ virtual const char* what() const throw() { return "My exception happened"; } // what() is a virtual method of exception} myex; int main () { try { throw myex; } catch (exception& e) { cout << e.what() << endl; } return 0; }

  29. Using the exceptionClass • All exceptions thrown by components of the C++ Standard library throw exceptions derived from the std::exception class: • bad_alloc: thrown by new on allocation failure • bad_cast: thrown by dynamic_cast when fails with a referenced type • bad_exception: thrown when an exception type doesn't match any catchtry { int * myarray= new int[1000]; } catch (bad_alloc&) { cout << "Error allocating memory." << endl; }

  30. Multiple Throws and Catches • try-block typically throws any number ofexception values, of differing types • Of course only one exception thrown • Since throw statement ends try-block • But different types can be thrown • Each catch block only catches "one type" • Typical to place many catch-blocks after eachtry-block • To catch "all-possible" exceptions to be thrown

  31. Catching • Order of catch blocks important • Catch-blocks tried "in order" after try-block • First match handles it! • Consider:catch (…) { } • Called "catch-all", "default" exception handler • Catches any exception • Ensure catch-all placed AFTER more specificexceptions! • Or others will never be caught!

  32. Trivial Exception Classes • Consider:class DivideByZero{ } • No member variables • No member functions (except defaultconstructor) • Nothing but it’s name, which is enough • Might be "nothing to do" with exception value • Used simply to "get to" catch block • Can omit catch block parameter

  33. Throwing Exception in Function • If a function does not catch an exception encountered during its execution, the function itself will throw the exception • It is then expected that its calling function will catch the exception • Callers might have different "reactions" • Some might desire to "end program" • Some might continue, or do something else • Makes sense to "catch" exception incalling function’s try-catch-block • Place call inside try-block • Handle in catch-block after try-block

  34. Throwing Exception in Function Example • Consider:try{ quotient = safeDivide(num, den);}catch (DivideByZero){ … } • safeDivide() function throws DividebyZero exception • Handled back in caller’s catch-block

  35. Function Exception Specification • Functions that don’t catch exceptions • Should "warn" users that it could throw • But it won’t catch! • Should list such exceptions:double safeDivide(int top, int bottom)throw (DividebyZero); • Called "exception specification" or "throw list" • Should be same in declaration and definition • All types listed handled "normally" • If no throw list  all exception types considered there • WARNING: some compilers do not process the throw list…

  36. Throw List • If exception thrown in function NOT inthrow list: • No errors (compile or run-time) • Function unexpected() automatically called • Default behavior is to terminate • Can modify behavior • Same result if no catch-block found

  37. Throw List Summary • void someFunction() throw(DividebyZero, OtherException); • Exception types DividebyZero or OtherException treated normally. All others invoke unexpected() • void someFunction() throw (); • Empty exception list. The function is not allowed to throw any exception. Thus, all exceptions thrown by the function invoke unexpected() • void someFunction(); • All exceptions of all types treated normally using a catch block, i.e. behaves as with a throw() containing all possible kinds of exceptions

  38. Derived Classes • Remember: derived class objects are alsoobjects of base class • Consider:D is derived class of B • If B is in exception specification  • Class D thrown objects will also be treatednormally, since it’s also object of class B • Note: does not do automatic type cast: • double will not account for throwing an int

  39. unexpected() • Default action: terminates program • No special includes or using directives • Normally no need to redefine • But you can: • Use set_unexpected • Consult compiler manual or advancedtext for details

  40. When to Throw Exceptions • Typical to separate throws and catches in separate functions • Throwing function: • Include throw statements in definition • List exceptions in throw list • In both declaration and definition • Catching function: • Different function, perhaps even in different file

  41. Preferred throw-catch Triad: throw void functionA() throw(MyException){ … throw MyException(arg); …} • Function throws exception as needed

  42. Preferred throw-catch Triad: catch • Then some other function:void functionB(){ … try { … functionA(); … } catch (MyException e) { // Handle exception } …}

  43. Uncaught Exceptions • Should catch every exception thrown • If not  program terminates • terminate() is called • Recall for functions • If exception not in throw list: unexpected()is called • It in turn calls terminate() • So same result

  44. Overuse of Exceptions • Exceptions alter flow of control • Similar to old "goto" construct • "Unrestricted" flow of control • Should be used sparingly • Good rule: • If desire a "throw": consider how to writeprogram without throw • If alternative reasonable  do it

  45. Exception Class Hierarchies • Useful to have; consider:DivideByZero class derives from:ArithmeticError exception class • All catch-blocks for ArithmeticError also catch DivideByZero • If ArithmeticError in throw list, thenDividebyZero also considered there

  46. Testing Available Memory • new operator throws bad_alloc exceptionif insufficient memory:try{ NodePtr pointer = new Node;}catch (bad_alloc){ cout << "Ran out of memory!"; // Can do other things here as well…} • In library <new>, std namespace:#include <new>using std::bad_alloc;

  47. Rethrowing an Exception • Legal to throw an exception IN catch-block! • Typically only in rare cases • Can even have a catch block re-throw its exception • throw; : Throws same exception again

  48. Summary 1 • Exception handling allows separation of"normal" cases and "exceptional" cases • Exceptions thrown in try-block • Or within a function whose call is in try-block • Exceptions caught in catch-block • try-blocks typically followed by more thanone catch-block • List more specific exceptions first

  49. Summary 2 • Best used with separate functions • Especially considering callers might handle differently • Exceptions thrown in but not caught infunction, should be listed in throw list • Exceptions thrown but never caught program terminates • Resist overuse of exceptions • Unrestricted flow of control

  50. References • Walter Savitch, Absolute C++, 3rd Edition, Addison-Wesley, 2007. • http://www.cplusplus.com/doc/tutorial/exceptions.html

More Related