Handling Errors Exceptionally Well in C++ 在C++中良好地捕获意外的错误
生活随笔
收集整理的這篇文章主要介紹了
Handling Errors Exceptionally Well in C++ 在C++中良好地捕获意外的错误
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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++的一個特性是它的異常捕獲系統。異常 是程序的一個未曾預料到的環節,沒有代碼被明確地設計用來處理包含問題的代碼片斷。在C++中,異常處理是非常有用的。因為它使得錯誤處理代碼從程序的正常代碼中分離出來。從而使得代碼容易書寫并便于閱讀。
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++的異常捕獲一直傳達到棧中。因此,如果有一系列的函數調用,但是僅有一個函數需要進行錯誤處理,C++中使用的異常捕獲意味著它可以很容易地來處理那些異常,而不需要在函數內部書寫專門的代碼。其影響是:函數不需要返回錯誤代碼,這為程序邏輯解放了它們的返回值。
When errors occur, the function generating the error can 'throw' an exception. For example, take a sample function that does division:
錯誤發生的時候,產生錯誤的函數會“拋出”一個異常。以一個做除法運算的函數為例來說明:
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:
這個函數會返回“除數為0”的錯誤。這個異常隨后會被專門捕獲int型異常的異常捕獲語句捕獲。捕獲異常必須的構件是一個try catch系統。如果你希望你的程序接收異常檢查,你必須將可能有異常捕獲的代碼包含在一個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語句捕獲特定類型的異常。例如,你可以拋出一個類的幾個對象來區分一些不同的異常。同時,一旦執行了一個catch語句,程序會從catch塊的后面繼續執行。
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.
創建一個類來保存錯誤發生時的信息是常常是很有用的。例如,如果你用一個類來處理異常:
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.
如果你想捕獲超過一個可能的異常,你可以為每個類型的異常指定獨立的catch塊。還可用一個通用的異常句柄來響應拋出的任何異常。要使用它,只需要為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.
關于異常捕獲順便要提及的是:錯誤可以在正常代碼之外被處理。這意味著很容易實現結構化的程序代碼,而且它使得錯誤處理更集中。最后,因為異常被傳回一直到主調函數的棧,你可以在(被調用函數)的任何地方捕獲錯誤。
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中,你可能會看到一些錯誤捕獲代碼,這些代碼用來釋放內存和關閉文件。它們被重復若干次,在每一個可能發生錯誤的地方。一些程序員偏愛的一種解決方法是使用goto語句。goto語句可以直接跳到執行清除操作的代碼部分。現在,你可以僅僅把你的代碼包圍在一個try-catch塊中并在catch之后執行一個清除操作。(可能要在catch塊中保存你的清理機制的一份拷備,如果你愿意將異常再次拋出以通知主調函數一個錯誤。)
在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++的一個特性是它的異常捕獲系統。異常 是程序的一個未曾預料到的環節,沒有代碼被明確地設計用來處理包含問題的代碼片斷。在C++中,異常處理是非常有用的。因為它使得錯誤處理代碼從程序的正常代碼中分離出來。從而使得代碼容易書寫并便于閱讀。
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++的異常捕獲一直傳達到棧中。因此,如果有一系列的函數調用,但是僅有一個函數需要進行錯誤處理,C++中使用的異常捕獲意味著它可以很容易地來處理那些異常,而不需要在函數內部書寫專門的代碼。其影響是:函數不需要返回錯誤代碼,這為程序邏輯解放了它們的返回值。
When errors occur, the function generating the error can 'throw' an exception. For example, take a sample function that does division:
錯誤發生的時候,產生錯誤的函數會“拋出”一個異常。以一個做除法運算的函數為例來說明:
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:
這個函數會返回“除數為0”的錯誤。這個異常隨后會被專門捕獲int型異常的異常捕獲語句捕獲。捕獲異常必須的構件是一個try catch系統。如果你希望你的程序接收異常檢查,你必須將可能有異常捕獲的代碼包含在一個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語句捕獲特定類型的異常。例如,你可以拋出一個類的幾個對象來區分一些不同的異常。同時,一旦執行了一個catch語句,程序會從catch塊的后面繼續執行。
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.
創建一個類來保存錯誤發生時的信息是常常是很有用的。例如,如果你用一個類來處理異常:
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.
如果你想捕獲超過一個可能的異常,你可以為每個類型的異常指定獨立的catch塊。還可用一個通用的異常句柄來響應拋出的任何異常。要使用它,只需要為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.
關于異常捕獲順便要提及的是:錯誤可以在正常代碼之外被處理。這意味著很容易實現結構化的程序代碼,而且它使得錯誤處理更集中。最后,因為異常被傳回一直到主調函數的棧,你可以在(被調用函數)的任何地方捕獲錯誤。
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中,你可能會看到一些錯誤捕獲代碼,這些代碼用來釋放內存和關閉文件。它們被重復若干次,在每一個可能發生錯誤的地方。一些程序員偏愛的一種解決方法是使用goto語句。goto語句可以直接跳到執行清除操作的代碼部分。現在,你可以僅僅把你的代碼包圍在一個try-catch塊中并在catch之后執行一個清除操作。(可能要在catch塊中保存你的清理機制的一份拷備,如果你愿意將異常再次拋出以通知主調函數一個錯誤。)
轉載于:https://www.cnblogs.com/diylab/archive/2007/09/05/883533.html
總結
以上是生活随笔為你收集整理的Handling Errors Exceptionally Well in C++ 在C++中良好地捕获意外的错误的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 重磅干货整理】机器学习(Machine
- 下一篇: 4. 用MVC实现URL路由