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.

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