C ++ catch枚举值作为异常 [英] C++ catch enum value as exception

查看:193
本文介绍了C ++ catch枚举值作为异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用已定义其例外的外部 c / c ++ 库:

 枚举MY_ERRORS {
ERR_NONE = 0,
ERR_T1,
ERR_T2,
}然后在代码中抛出异常如下:

  if(...){
throw ERR_T1;

作为C ++中的编程的新手,我会做一些事情:

  try {
call_to_external_library();
} catch(??? err){
printf(发生错误:%s\\\
,err);
} catch(...){
printf(发生意外异常。\\\
);
}

如何确定引发的内容? / p>

解决方案

您需要编写代码来处理catch块中的枚举类型:

  try {
call_to_external_library();
} catch(MY_ERRORS err){//< ------------------------ HERE
printf(An error occurred :%s\\\
,err);
} catch(...){
printf(发生意外异常。\\\
);
}


I am trying to use an external C++ library which have defined its exceptions as:

enum MY_ERRORS {
    ERR_NONE = 0,
    ERR_T1,
    ERR_T2,
};

Then in the code exceptions are thrown like this:

if(...) {
    throw ERR_T1;

Being new to programming in C++, I would do something like:

try {
    call_to_external_library();
} catch(??? err) {
    printf("An error occurred: %s\n", err);
} catch(...) {
    printf("An unexpected exception occurred.\n");
}

How do I determine what was thrown?

解决方案

You will need to write your code to handle the type of enumeration in the catch block:

try {
    call_to_external_library();
} catch(MY_ERRORS err) {      // <------------------------ HERE
    printf("An error occurred: %s\n", err);
} catch(...) {
    printf("An unexpected exception occurred.\n");
}

这篇关于C ++ catch枚举值作为异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆