Gillius's Programming Forums

Hosted Forum: West Visayas State University - Philippines => Problem Solving and Programming Class Forum => Topic started by: louiecerv on March 05, 2006, 05:41:42 AM

Title: try-throw-catch
Post by: louiecerv on March 05, 2006, 05:41:42 AM
The basic mechanism for throwing and catching exceptions is a try-throw-catch sequence.  The throw statement throws the exception (value). The catch block catches the exception (the value).  When an exception is thrown, the try block ends and then the code in the catch block is executed.  After the catch block is completed, the code after the catch block or blocks is executed (provided the catch block has not ended the program or performed some other special action.

The sample code is provided as example to learn the syntax but not as example of good or typical use of exception handling.
Title: Define your own exception class
Post by: louiecerv on March 05, 2006, 06:33:55 AM
A better way to thrown information to the catch block is by means of an exception class.

An exception class is no different from other classes except in the way it is used.  The main reason for using an exception class is so that you can have a different type to identify each possible kind of exception situation.
Title: multiple throws and catches
Post by: louiecerv on March 05, 2006, 06:47:54 AM
A try block can potentially throw any number of exception values, which can be of different types.  Each catch block can only catch values of one type, but you can catch exception values of different types by placing more than one catch block after a try block.  

In this example, there is no paramater in the catch block for DivideByZero. If you do not need a parameter, you can simply list the type with no parameter.
Title: try-throw-catch
Post by: Gillius on March 09, 2006, 09:41:19 AM
When making exception classes, it can be beneficial to inherit from the C++ exception hierarchy already in place, such as std::exception.  I've been known to not do this before, but I know that I should probably do this.  In Java it is required, but in C++ you can throw anything.

Also, catching const reference of objects can be better than catching copies as it prevents a copy-on-throw and allows throwing of non-copyable objects.  If you put prints in the ctor and copy functions of a class you can see how the object is copied and destroyed multiple times when catching-by-value rather than catching-by-const-reference.

A "bonus" mention:
An interesting note about catch(...) in Microsoft Visual Studio is that SEH exceptions (an old exception-handling mechanism in Windows) such as a true seg fault or divide by zero will be caught in a catch(...).  An unhandled SEH exception is what causes that "This program has performed an illegal operation and will be closed" box, so if you have a catch(...) it will catch EVERYTHING.  A problem with this is that debugger breakpoints are implemented with SEH so you can actually inadvertently disable your debugger with a catch(...), so if you are running a debugger on your code and it is "ignoring" your breakpoints you will want to check for catch(...) blocks.