Handling Errors Exceptionally Well in C++ 在C++中良好地捕获意外的错误
生活随笔
收集整理的這篇文章主要介紹了
Handling Errors Exceptionally Well in C++ 在C++中良好地捕获意外的错误
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Handling Errors Exceptionally Well in C++
在C++中良好地捕獲意外的錯誤
from:http://www.cprogramming.com/tutorial/exceptions.html
author:unknown
翻譯:范晨鵬
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.
C++的一個特性是它的異常捕獲系統(tǒng)。異常 是程序的一個未曾預(yù)料到的環(huán)節(jié),沒有代碼被明確地設(shè)計(jì)用來處理包含問題的代碼片斷。在C++中,異常處理是非常有用的。因?yàn)樗沟缅e誤處理代碼從程序的正常代碼中分離出來。從而使得代碼容易書寫并便于閱讀。
Furthermore, exception handling in C++ propagates the exceptions up the stack; therefore, if there are several functions 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.
而且,C++的異常捕獲一直傳達(dá)到棧中。因此,如果有一系列的函數(shù)調(diào)用,但是僅有一個函數(shù)需要進(jìn)行錯誤處理,C++中使用的異常捕獲意味著它可以很容易地來處理那些異常,而不需要在函數(shù)內(nèi)部書寫專門的代碼。其影響是:函數(shù)不需要返回錯誤代碼,這為程序邏輯解放了它們的返回值。
When errors occur, the function generating the error can 'throw' an exception. For example, take a sample function that does division:
錯誤發(fā)生的時候,產(chǎn)生錯誤的函數(shù)會“拋出”一個異常。以一個做除法運(yùn)算的函數(shù)為例來說明:
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:
這個函數(shù)會返回“除數(shù)為0”的錯誤。這個異常隨后會被專門捕獲int型異常的異常捕獲語句捕獲。捕獲異常必須的構(gòu)件是一個try catch系統(tǒng)。如果你希望你的程序接收異常檢查,你必須將可能有異常捕獲的代碼包含在一個try塊中。例如:
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語句捕獲特定類型的異常。例如,你可以拋出一個類的幾個對象來區(qū)分一些不同的異常。同時,一旦執(zhí)行了一個catch語句,程序會從catch塊的后面繼續(xù)執(zhí)行。
It is often more useful for you to create a class that stores information on exceptions as they occur. For example, it would be more useful if you had a class to handle exceptions.
創(chuàng)建一個類來保存錯誤發(fā)生時的信息是常常是很有用的。例如,如果你用一個類來處理異常:
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.
如果你想捕獲超過一個可能的異常,你可以為每個類型的異常指定獨(dú)立的catch塊。還可用一個通用的異常句柄來響應(yīng)拋出的任何異常。要使用它,只需要為catch語句使用catch(...)來打印一個通用的警告語句或使用類似的處理方式。
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.
關(guān)于異常捕獲順便要提及的是:錯誤可以在正常代碼之外被處理。這意味著很容易實(shí)現(xiàn)結(jié)構(gòu)化的程序代碼,而且它使得錯誤處理更集中。最后,因?yàn)楫惓1粋骰匾恢钡街髡{(diào)函數(shù)的棧,你可以在(被調(diào)用函數(shù))的任何地方捕獲錯誤。
In C, you might see some error handling code to free memory and close files repeated five or 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).
在C中,你可能會看到一些錯誤捕獲代碼,這些代碼用來釋放內(nèi)存和關(guān)閉文件。它們被重復(fù)若干次,在每一個可能發(fā)生錯誤的地方。一些程序員偏愛的一種解決方法是使用goto語句。goto語句可以直接跳到執(zhí)行清除操作的代碼部分。現(xiàn)在,你可以僅僅把你的代碼包圍在一個try-catch塊中并在catch之后執(zhí)行一個清除操作。(可能要在catch塊中保存你的清理機(jī)制的一份拷備,如果你愿意將異常再次拋出以通知主調(diào)函數(shù)一個錯誤。)
在C++中良好地捕獲意外的錯誤
from:http://www.cprogramming.com/tutorial/exceptions.html
author:unknown
翻譯:范晨鵬
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.
C++的一個特性是它的異常捕獲系統(tǒng)。異常 是程序的一個未曾預(yù)料到的環(huán)節(jié),沒有代碼被明確地設(shè)計(jì)用來處理包含問題的代碼片斷。在C++中,異常處理是非常有用的。因?yàn)樗沟缅e誤處理代碼從程序的正常代碼中分離出來。從而使得代碼容易書寫并便于閱讀。
Furthermore, exception handling in C++ propagates the exceptions up the stack; therefore, if there are several functions 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.
而且,C++的異常捕獲一直傳達(dá)到棧中。因此,如果有一系列的函數(shù)調(diào)用,但是僅有一個函數(shù)需要進(jìn)行錯誤處理,C++中使用的異常捕獲意味著它可以很容易地來處理那些異常,而不需要在函數(shù)內(nèi)部書寫專門的代碼。其影響是:函數(shù)不需要返回錯誤代碼,這為程序邏輯解放了它們的返回值。
When errors occur, the function generating the error can 'throw' an exception. For example, take a sample function that does division:
錯誤發(fā)生的時候,產(chǎn)生錯誤的函數(shù)會“拋出”一個異常。以一個做除法運(yùn)算的函數(shù)為例來說明:
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:
這個函數(shù)會返回“除數(shù)為0”的錯誤。這個異常隨后會被專門捕獲int型異常的異常捕獲語句捕獲。捕獲異常必須的構(gòu)件是一個try catch系統(tǒng)。如果你希望你的程序接收異常檢查,你必須將可能有異常捕獲的代碼包含在一個try塊中。例如:
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語句捕獲特定類型的異常。例如,你可以拋出一個類的幾個對象來區(qū)分一些不同的異常。同時,一旦執(zhí)行了一個catch語句,程序會從catch塊的后面繼續(xù)執(zhí)行。
It is often more useful for you to create a class that stores information on exceptions as they occur. For example, it would be more useful if you had a class to handle exceptions.
創(chuàng)建一個類來保存錯誤發(fā)生時的信息是常常是很有用的。例如,如果你用一個類來處理異常:
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.
如果你想捕獲超過一個可能的異常,你可以為每個類型的異常指定獨(dú)立的catch塊。還可用一個通用的異常句柄來響應(yīng)拋出的任何異常。要使用它,只需要為catch語句使用catch(...)來打印一個通用的警告語句或使用類似的處理方式。
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.
關(guān)于異常捕獲順便要提及的是:錯誤可以在正常代碼之外被處理。這意味著很容易實(shí)現(xiàn)結(jié)構(gòu)化的程序代碼,而且它使得錯誤處理更集中。最后,因?yàn)楫惓1粋骰匾恢钡街髡{(diào)函數(shù)的棧,你可以在(被調(diào)用函數(shù))的任何地方捕獲錯誤。
In C, you might see some error handling code to free memory and close files repeated five or 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).
在C中,你可能會看到一些錯誤捕獲代碼,這些代碼用來釋放內(nèi)存和關(guān)閉文件。它們被重復(fù)若干次,在每一個可能發(fā)生錯誤的地方。一些程序員偏愛的一種解決方法是使用goto語句。goto語句可以直接跳到執(zhí)行清除操作的代碼部分。現(xiàn)在,你可以僅僅把你的代碼包圍在一個try-catch塊中并在catch之后執(zhí)行一個清除操作。(可能要在catch塊中保存你的清理機(jī)制的一份拷備,如果你愿意將異常再次拋出以通知主調(diào)函數(shù)一個錯誤。)
轉(zhuǎn)載于:https://www.cnblogs.com/diylab/archive/2007/09/05/883533.html
總結(jié)
以上是生活随笔為你收集整理的Handling Errors Exceptionally Well in C++ 在C++中良好地捕获意外的错误的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 重磅干货整理】机器学习(Machine
- 下一篇: 移动魔盒cm201-2原厂备份固件hit