【问题标题】:Try-Catch Problem in c++c++中的Try-Catch问题
【发布时间】:2011-09-26 19:47:25
【问题描述】:

我正在尝试用 C++ 实现队列。在此期间我遇到了这个问题。

void Queue::view() 
{
 int i;
 try
 {
  if(Qstatus==EMPTY)
  {
    UnderFlowException ex = UnderFlowException("\nQUEUE IS EMPTY");
    throw ex;
  }
 }

 i=front;
 cout<<"Queue contains...\n";

 while(i <= rear)
 {
  cout<<queue[i]<<" ";
  i++;
 }
}

这给出了一个错误:

错误:在“i”之前预期“catch”

我认为出现这个问题是因为我没有在 try 块下面写 catch 块。 但是如果想在 main() 中编写 catch 块,(就像在这种情况下一样),我该怎么做呢?

以前,那我可以这样做吗?如果不是为什么?

【问题讨论】:

  • 与问题无关,但您的 while 循环将永远运行,除非 queue[i] 增加 i(这不太可能)。
  • 我正要在 SO 上第一次回答 :) 但是这些多核用户..
  • 另一个问题可能是你为什么要添加try 块?如果是因为您预计会引发异常,那么catch 似乎很明显丢失了。但我感觉你不太清楚 try catch 语句的用途,如果是这种情况,你可能想看看这里:cplusplus.com/doc/tutorial/exceptions
  • 如果你写UnderFlowException ex("\nQUEUE IS EMPTY");看起来更干净
  • 一个视图方法可能一开始就不应该抛出任何东西......为什么想要输出一个空队列会出错?更好地为非空队列是前提条件(pop)的操作保留 UnderFlowError。

标签: c++ exception-handling try-catch


【解决方案1】:

catch 块必须跟在 try 块之后。如果您希望 catch 出现在 main 中 - 那也是 try 必须出现的地方。您可以在任何地方使用throw,而不必在同一函数内的try 块内。

应该是这样的:

void Queue::view() 
{
 int i;
 if(Qstatus==EMPTY)
 {
    UnderFlowException ex = UnderFlowException("\nQUEUE IS EMPTY");
    throw ex;
 }

 i=front;
 cout<<"Queue contains...\n";

 while(i <= rear)
  cout<<queue[i]<<" ";
}
/// ...
int main()
{
    Queue q;
    try{
       q.view();
    }
    catch(UnderFlowException ex)
    {
        /// handle
    }
    catch (...)
    {
     /// unexpected exceptions
    }
    // follow the success/handled errors
}

【讨论】:

    【解决方案2】:

    您只需删除try 块。 try 块始终与 catch 一起使用。

    void Queue::view() 
    {
        int i;
        if(Qstatus==EMPTY)
        {
            ex = UnderFlowException("\nQUEUE IS EMPTY");
            throw ex;
        }
    
        i=front;
        cout<<"Queue contains...\n";
    
        while(i <= rear)
            cout<<queue[i]<<" ";
    }
    

    然后您可以在您的main 中包含try/catch 构造。

    int main()
    {
        Queue queue;
        try
        {
            queue.View()
        }
        catch(UnderFlowException ex)
        {
            //handle ex
        }
        return 0;
    }
    

    【讨论】:

      【解决方案3】:

      所有 try 块都需要至少一个关联的 catch 块。如果您无意在此处处理任何异常,则应删除 try 块。可以(通常应该!)在 try 块之外抛出异常。

      【讨论】:

        【解决方案4】:

        让您的代码捕获并重新抛出异常,如下所示:

        try
        {
         if(Qstatus==EMPTY)
         {
           UnderFlowException ex = UnderFlowException("\nQUEUE IS EMPTY");
           throw ex;
         }
        } catch( ... ) {
           throw; // rethrow whatever exception we just catched
        }
        

        虽然你一开始甚至不需要try 块。看起来只需要throw ex; 就可以了,因为你不打算抓住它而只是扔掉它。

        【讨论】:

        • 抓不抓,为什么要抓?
        • 这个 try-catch 是代码膨胀,除了可能减慢程序的速度之外什么也没做。删除 try 块是更好的解决方案。
        • @David Heffernan:嗯,这就是他如何做到的,​​尽管这样做似乎没有意义。
        • view 方法可能一开始就不应该抛出任何东西......为什么输出空队列会出错?
        • @UncleBens:好吧,我不是在暗示他的逻辑,虽然我不同意它......对 OP 问题发表评论,以便他注意到它。
        【解决方案5】:
        try{
        }
        catch(Exception ex){
        }
        

        Catch 必须在 try 之后立即进行。这些是规则。

        【讨论】:

          猜你喜欢
          • 2011-07-04
          • 2011-02-23
          • 1970-01-01
          • 2011-11-28
          • 2011-08-26
          • 2011-09-10
          • 1970-01-01
          • 1970-01-01
          • 2020-06-19
          相关资源
          最近更新 更多