|
What are Exceptions?I will briefly cover what exceptions are here. I will not delve too deeply here since any C++ book will cover the syntax better than I can. This is meant to be a refresher course. Please consult a book for syntax details. Exceptions are designed for error handling. There are two kinds of exceptions - hardware or software. The hardware exception occurs when you do a divide by zero, access invalid pointers, and so on. Dealing with hardware exceptions is usually platform dependant, and the C++ standard has no rules on how hardware exceptions are to be dealt with. This is where our discussion will be Windows dependant. Example int x = 3, y = 0; int z = x / y; // this will cause a hardware divide by 0 exception Hardware exceptions normally occur because of program bugs. We cannot really do much with them except handling them so we will leave this aside and focus on software exceptions first. Try/catch/throwSoftware exceptions are generated by the programmer using the keywords try, catch and throw. The try keyword is to guard a section of code against an exception; the catch keyword is to handle the exception; the throw keyword is to generate an exception. Example
We can throw any type of exception - an int, a string, a class object. I generate an int exception here for simplicity. Later, we will generate a class object exception to identify the type of error. The rule is we should catch whatever we throw. How to try/catchTherefore, the try/catch section is normally placed in the main section. This will ensure the program will never crash (well almost). When a function encounters an error situation that should end the program, it throws an exception. Example
Note that there is only one try/catch in the main function. Functions in the program do not need try/catch blocks. You should note also that generating an exception would stop the program flow in the caller. So if a function calls a function that generates an exception, the code below the calling function would not execute. Example
This implies that exceptions should only be used to for serious errors and that the program should quit, hence the name Exceptions. Throwing an exception requires a catch handler so if the error is common, then a return code would be more appropriate. The rule is to only throw exceptions for critical errors and limit your try/catch to the main program function only. Exceptions are errors but errors are not necessarily exceptions. RethrowSometimes you would like to catch the exception but still propagate the exception upwards. This is known as re-throwing the exception and is done with the throw keyword in a catch handler. Example
This technique is normally used when the function needs to know about a specific type of exception but is not ready to handle the exception yet. I will show you how to use such a method later but you should avoid this technique if possible. That should be about all you need to know about exception syntax. All of these should be readily available in a good C++ book. |