如果throw函数抛出多个同类型参量,如何用catch函数捕捉?例如:if(……)throw“error1”; else if (……) throw“error2”;else throw“error3”;try(……){ // };catch(char *){ } catch 函数体如何写才能分

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/11 01:20:51
如果throw函数抛出多个同类型参量,如何用catch函数捕捉?例如:if(……)throw“error1”; else if (……) throw“error2”;else throw“error3”;try(……){ // };catch(char *){ } catch 函数体如何写才能分

如果throw函数抛出多个同类型参量,如何用catch函数捕捉?例如:if(……)throw“error1”; else if (……) throw“error2”;else throw“error3”;try(……){ // };catch(char *){ } catch 函数体如何写才能分
如果throw函数抛出多个同类型参量,如何用catch函数捕捉?
例如:
if(……)throw“error1”; else if (……) throw“error2”;else throw“error3”;
try(……){ // };catch(char *){ } catch 函数体如何写才能分辨性的输出是哪一种错误?

如果throw函数抛出多个同类型参量,如何用catch函数捕捉?例如:if(……)throw“error1”; else if (……) throw“error2”;else throw“error3”;try(……){ // };catch(char *){ } catch 函数体如何写才能分
这个时候catch函数的参数就不能仅仅是数据类型了 还要加上变量名,此时catch函数就可以获得throw的具体内容:
catch (char * str)
{
if(strcmp(str,"error1")==0)
//
else if(strcmp(str,"error2")==0
//
else
//
}