C++ Exception catching and exception handling

同學建議,在寫博客的時候將對應的中文也補充上,可以增加博客的可讀性,後面加入這部分,如果您有更好的建議,可以給我留言或者私信我,非常感謝您的反饋。

  • --->The students suggested that the corresponding Chinese should be added when writing the blog to improve the readability of the blog. If you have better Suggestions, please leave me a message or send me a private message. Thank you very much for your feedback.

今天,主要介紹一下在本次項目中使用C++進行異常捕獲處理的問題。

  • ---->Today, I'll focus on the use of C++ for exception catching and handling in this project.

當你知道你的異常是什麼的時候,你可以用下面的這個方法進行處理。

  • ---->When you know what your exception is, you can use the following method to handle it.
try
{

}
catch(ExceptionName e1)
{

}
catch(ExceptionName e2)
{

}

 你能在try關鍵字下面寫入你自己需要異常處理的代碼,catch下面是有異常進行處理的代碼。

  • ---->You can write your own code that needs exception handling under the try keyword, and the code that needs exception handling under catch.

try: 需要異常處理代碼。

catch: catch 用於捕獲異常。

throw: 通過使用 throw 關鍵字來完成拋出一個異常。

  • ---->try:Exception handling code is required.
  • ---->catch:Catch is used to catch exceptions.
  • ---->throw:Throwing an exception by using the throw keyword.

 throw 語句在代碼塊中的任何地方拋出異常。throw 語句的操作數可以是任意的表達式,表達式的結果的類型決定了拋出的異常的類型。catch語句可以據此捕捉該類型異常。

  • ---->throw:The throw statement throws an exception anywhere in the code block.The operands of the throw statement can be any expression, and the type of the result of the expression determines the type of exception thrown.The catch statement can catch this type of exception accordingly.

今天,在最近項目我使用了下面的異常處理代碼,可以參考:

  • ---->Today,In the recent project, I used the following exception handling code, which can be referred to:
try
{
   throw "error";
}
catch (char *allcatch)
{
   //cout << "catched:" << e.what() << endl;
   QDateTime current_date_time = QDateTime::currentDateTime();
   QString current_date = current_date_time.toString("yyyy-MM-dd hh:mm:ss ddd");
   QString fileName = ".//*.log";
   QString str = current_date + "----->"+"ALL-catched:"+allcatch+"\r\n";
   QFile file(fileName);
   if (!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))
   {
	   ;
   }
   QTextStream in(&file);
   in << str << "\n";
   file.close();
}

這個可以實現所有異常的捕獲。

  • ---->This allows all exceptions to be caught.

介紹一個之前使用的異常捕獲方式。我將異常的結果寫入log日誌文件 :參考下面的代碼:

  • ---->To introduce an exception catching method used previously。I write the result of the exception to the log file:refer to the following code:
try
{
    throw "error";
}
catch (exception& e)
{
	cout << "catched:" << e.what() << endl;
	QDateTime current_date_time = QDateTime::currentDateTime();
	QString current_date = current_date_time.toString("yyyy-MM-dd hh:mm:ss ddd");
	QString fileName = ".//*.log";
	QString str = current_date + "----->"+"--catched:"+e.what()+"\r\n";
	QFile file(fileName);
	if (!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))
	{
		;
	}
	QTextStream in(&file);
	in << str << "\n";
	file.close();
}

這個只能捕獲標準異常,不能捕獲所有的異常。

  •  ---->This only catches standard exceptions, not all exceptions.

我的建議,儘量全的考慮異常處理,能在程序中處理的就在程序中處理,使用異常捕獲只能說是不靠譜,有一些異常直接是捕獲不到的,而且編譯器不同,debug和release版本下會有不同的效果。建議儘可能多的考慮全,使用異常捕獲只能說是備選。

  •  ---->It can be handled  is handled in the program. Using exception catched can only be considered as unreliable. Some exceptions cannot be captured directly.It is recommended that you consider as much as possible, using exception capture as an alternative.

​​​I hope I can help you,If you have any questions, please  comment on this blog or send me a private message. I will reply in my free time.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章