【问题标题】:How to make a try catch recursion in a switch statement iterative?如何在 switch 语句迭代中进行 try catch 递归?
【发布时间】:2016-10-19 02:05:36
【问题描述】:

我正在尝试制作如下所示的功能。我想让它迭代,但我不确定,因为我需要保持所有路径打开。

someFunction( parameters)
{
   //do stuff

   switch( thing )
   case: one
    if (something true)
     { 
         try {  someFunction( different parameters ) } catch(...) { throw }

   case: two

     //if else with the else being a throw exception. 
   case: three

     // same
   case: four 
    ...

   default

return some value

【问题讨论】:

    标签: c++ exception recursion iteration try-catch


    【解决方案1】:

    我不太了解你的问题,但我会尽力提供帮助!

    如何使用例外:

    ...
    try
    {
    if(b == 0)
        throw("A number cannot be devided by zero!\n");
    c = a/b
    }
    catch(string error)
    {
          cout << "ERROR: "<< error << endl;
    }
    return c
    ...
    

    你可以在 switch 语句中使用它,在 catch 之后我将返回到 case。

    switch(c)
    {
    case 0:
        ...
        try { ... throw();}
        catch() { ... error treatment}
        ...
        break;
    case 1:
    ...
    }
    

    【讨论】:

    • 我对异常有点了解,但我很难找到一种适合将此递归函数变为迭代函数的方法,而无需删除异常并在迭代期间保持所有路径打开。
    猜你喜欢
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2020-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多