Está en la página 1de 3

Foundations of Exceptions

Introduction
During the execution of a program, the computer will face two types of situations: those it is prepared to deal with and those it doesnt like. Imagine you write a program that asks the user to supply two numbers to perform a calculation. Here is such a program: #include <iostream> using namespace std; int main() { double a, b, c; // Request two numbers from the user cout << "Please provide two numbers\n"; cout << "First Number: "; cin >> a; cout << "Second Number: "; cin >> b; // Multiply the numbers and display the result c = a * b; cout << "\n" << a << " * " << b << " = " << c << "\n\n"; return 0; } This is a classic easy program. When it comes up, the user is asked to simply type two numbers; the program would use them to perform a multiplication and display the result. Imagine that a user, thanks to his infinite creativity or because of just a mistake, decides to type the name of a country or somebodys telephone number as one of the requested values. Since a program such as this one is not prepared to multiply two strings or one number to a string (actually, using operator overloading, you can tell the compiler how to perform almost any type of operation on the values of your program), it would not know what to do. The only alternative the compiler would have is to send the problem to the operating system, hoping that the OS would know what to do. What actually happens is that, whenever the compiler is handed a task, it would try to perform the assignment. If it cant perform the assignment, for any reason it is not prepared for, it would throw an error. As a programmer, if you can anticipate the type oferror that could occur in your program, you can catch the error yourself and deal with it by telling the compiler what to do when this type of error occurs.

Exceptional Behaviors
An exception is a situation that would be unusual for the program that is being processed. As a programmer, you should anticipate any abnormal behavior that could be caused by the user entering wrong information that could otherwise lead to unpredictable results. An error result or an unpredictable behavior on your program not caused by the operating systembut that occurs in your program is called an exception. The ability to deal with a programs eventual abnormal behavior is called exception handling. C++ provides three

keywords to handle an exception. 1. Trying the normal flow: To deal with the expected behavior of a program, use the trykeyword as in the following syntax: try {Behavior} The try keyword is required. It lets the compiler know that you are anticipating an abnormal behavior and will try to deal with it. The actual behavior that needs to be evaluated is included between an opening curly bracket { and a closing curly bracket }. Inside of the brackets, implement the normal flow that the program should follow, at least for this section of the code. 2. Catching Errors: During the flow of the program as part of the try section, if an abnormal behavior occurs, instead of letting the program crash or instead of letting the compiler send the error to the operating system, you can transfer the flow of the program to another section that can deal with it. The syntax used by this section is: catch(Argument) {WhatToDo}
This section always follows the try section and there must not be any code between the trys closing bracket and the catch section. The catch keyword is required and follows the try section. The catch behaves a little like a function. It uses an argument that is passed by the previous try section. The argument can be a regular variable or a class. If there is no argument to pass, thecatch must at least take a three-period argument as in catch(). The behavior of the catchclause starts with an opening curly bracket { and ends with a closing curly bracket }. The inside of the brackets is called the body of the catch clause. Therefore, use the body of the catchto deal with the error that was caused. Combined with the try block, the syntax of an exception would be:

try { // Try the program flow } catch(Argument) { // Catch the exception }


3. Throwing an error: There are two main ways an abnormal program behavior is transferred from the try block to the catch clause. This transfer is actually carried by the throw keyword. Unlike thetry and catch blocks, the throw keyword is independent of a formal syntax but still follows some rules.

Facing an Exception
An exception is a behavior that should not occur in your program but is likely to show up. The simplest exception looks like a conditional statement and here is an example: #include <iostream> using namespace std; int main() { int StudentAge; cout << "Student Age: "; cin >> StudentAge;

try { if(StudentAge < 0) throw; cout << "\nStudent Age: " << StudentAge << "\n\n"; } catch(...) { } cout << "\n"; return 0; } If you run this program and type a positive integer for the students age, the program would respond by displaying the student age. Thats a good outcome. If you run the program and type a letter or any character, the compiler would display the student age as 0. This is the first proof that the compilers are already configured to deal with some abnormal behavior of a program. When the throw keyword is written by itself, it is a way of asking the compiler to send the exception to another handler. In fact, if there is no other handler written by you, the processing would be handed to the operating system. In this case, if you run the program and type a negative integer, since the program is not prepared to handle the exception itself, because of the presence of a single throw, the operating system would take over and display its own message. This would be abnormal program termination on a Microsoft Windows operating system.

También podría gustarte