Handling errors exceptionally well in C ++ catch unexpected errors in C ++

Source: Internet
Author: User
Handling errors exceptionally well in C ++
Capture unexpected errors in C ++

From: http://www.cprogramming.com/tutorial/exceptions.html
Author: Unknown
Translation: Fan chenpeng
One benefit of C ++ over C is its exception handling system. an exception is a situation in which a program has an unexpected circumstance that the section of code containing the problem is not explicitly designed to handle. in C ++, exception handling is useful because it makes it easy to separate the error handling code from the code written to handle the chores of the program. doing so makes reading and writing the code easier.
One feature of C ++ is its exception capture system. Exceptions are an unexpected part of the program. No code is clearly designed to handle code snippets that contain problems. In C ++, exception handling is very useful. It separates the error handling code from the normal code of the program. This makes the code easy to write and easy to read.
Furthermore, exception handling in C ++ propagates the exceptions up the stack; therefore, if there are several funled called, but only one function that needs to reliably deal with errors, the method C ++ uses to handle exceptions means that it can easily handle those exceptions without any code in the intermediate functions. one consequence is that functions don't need To Return Error Codes, freeing their return values for program logic.
In addition, the exception capture of C ++ is always transmitted to the stack. Therefore, if there are a series of function calls, but only one function requires error handling, the exception capture used in C ++ means that it can easily handle those exceptions, instead of writing special code inside the function. The impact is that functions do not need to return error code, which frees program logic from their return values.

When errors occur, the function generating the error can 'throw' an exception. For example, take a sample function that does Division:
When an error occurs, the function that generates the error will throw an exception. Take a division function as an example:

Const int dividebyzero = 10;
//....
Double divide (Double X, Double Y)
{
If (y = 0)
{
Throw dividebyzero;
}
Return x/y;
}

The function will throw dividebyzero as an exception that can then be caught by an exception-handling catch statement that catches exceptions of Type Int. the necessary construction for catching exceptions is a try catch system. if you wish to have your program check for exceptions, you must enclose the code that may have exceptions thrown in a try block. for example:
This function returns the error "Division 0. This exception is subsequently captured by exception capture statements that specifically capture int-type exceptions. The component necessary to capture exceptions is a try catch system. If you want your program to receive exception checks, you must include code that may be caught by exceptions in a try block. For example:
Try
{
Divide (10, 0 );
}
Catch (int I)
{
If (I = dividebyzero)
{
Cerr <"divide by zero error ";
}
}

The catch statement catches exceptions that are of the proper type. you can, for example, throw objects of a class to differentiate between several different exceptions. as well, once a catch statement is executed, the program continues to run from the end of the catch.
Catch
Catch statements capture exceptions of specific types. For example, you can throw several objects of a class to distinguish some different exceptions. At the same time, once a catch statement is executed, the program continues to execute from behind the Catch Block.
It is often more useful for you to create a class that stores information on exceptions as they occur. For example, it wocould be more useful if you had a class to handle exceptions.

It is often useful to create a class to save the information when an error occurs. For example, if you use a class to handle exceptions:

Class dividebyzero
{
Public:
Double divisor;
Dividebyzero (Double X );
};
Dividebyzero: dividebyzero (Double X): divisor (X)
{}
Int divide (int x, int y)
{
If (y = 0)
{
Throw dividebyzero (X );
}
}
Try
{
Divide (12, 0 );
}
Catch (dividebyzero divzero)
{
Cerr <"attempted to divide" <divzero. divisor <"by zero ";
}
F you wish to catch more than one possible exception, you can specify separate catch blocks for each type of exception. it's also possible to have a general exception handler that will respond to any thrown exception. to use it, simply use catch (...) for the catch statement and print a general warning of some kind.
If you want to catch more than one possible exception, you can specify an independent Catch Block for each type of exception. You can also use a common exception handle to respond to any thrown exceptions. To use it, you only need to use catch (...) for the catch statement to print a general warning statement or use a similar processing method.
The handy thing to remember about exception handling is that the errors can be handled outside of the regular code. this means that it is easier to structure the program code, and it makes dealing with errors more centralized. finally, because the exception is passed back up the stack of calling functions, you can handle errors at any place you choose.
Exception capture by the way: errors can be handled out of normal code. This means it is easy to implement structured program code, and it makes error handling more concentrated. Finally, you can capture errors anywhere in the called function because exceptions are returned to the stack of the main function.
In C, you might see some error handling code to free memory and close files repeated five or six times, once for each possible error. A solution some programmers prefered was to use a GOTO statement that jumped all the way to the cleanup code. now, you can just surround your code with a try-Catch Block and handle any cleanup following the catch (possibly with an additional copy of your cleanup routine inside the Catch Block if you intend to throw the exception again to alert the calling function of an error ).
In C, you may see some error capture code, which is used to release the memory and close the file. They are repeated several times in every possible error. One of the preferred solutions for some programmers is to use the GOTO statement. The GOTO statement can directly jump to the Code section that executes the clear operation. Now, you can only enclose your code in a try-Catch Block and execute a clear operation after the catch. (You may need to save a copy of your cleanup mechanism in the Catch Block. If you are willing to throw an exception again to notify the main function of an error .)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.