异常捕获后确定异常类型? [英] Determining exception type after the exception is caught?
本文介绍了异常捕获后确定异常类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
示例:
$ b有没有办法确定异常类型$ b
try
{
SomeBigFunction();
}
catch(...)
{
//确定例外类型
}
解决方案
你可以在catch(...)中确定类型,但不是很有用:
#include< iostream>
#include< exception>
class E1:public std :: exception {};
class E2:public std :: exception {};
int main(){
try {
throw E2();
}
catch(...){
try {
throw;
}
catch(const E1& e){
std :: cout<< E1\\\
;
}
catch(const E2& e){
std :: cout<< E2\\\
;
}
}
}
Is there a way to determine the exception type even know you caught the exception with a catch all?
Example:
try
{
SomeBigFunction();
}
catch(...)
{
//Determine exception type here
}
解决方案
You can actully determine type inside a catch(...), but it is not very useful:
#include <iostream>
#include <exception>
class E1 : public std::exception {};
class E2 : public std::exception {};
int main() {
try {
throw E2();
}
catch( ... ) {
try {
throw;
}
catch( const E1 & e ) {
std::cout << "E1\n";
}
catch( const E2 & e ) {
std::cout << "E2\n";
}
}
}
这篇关于异常捕获后确定异常类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文