首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >代码中的这个错误是什么?

代码中的这个错误是什么?
EN

Stack Overflow用户
提问于 2011-09-27 09:40:09
回答 3查看 8.8K关注 0票数 1

在使用try-catch in时,遇到了这个错误。但是,尽管我上网和SO,我还是找不出这个错误的原因。

我的代码是...

代码语言:javascript
运行
复制
int main()
{
Queue q;
int choice,data;

 while(1)
 {
  choice  = getUserOption();
  switch(choice)
  {
   case 1:
     cout<<"\nEnter an element:";
     cin>>data;
     q.enqueue(data);
     break;
   case 2:
     int element;
     element = q.dequeue();
     cout<<"Element Dequeued:\n"<<element;
     break;
   case 3:
     q.view();
     break;
   case 4:
     exit(EXIT_SUCCESS);
  }
  catch(UnderFlowException e)
  {
   cout<<e.display();
  }
  catch(OverFlowException e)
  {
   cout<<e.display();
  }

 }// end of while(1)

       return 0;
}

对我来说,上面代码中的一切似乎都是正确的。但是g++编译器抛出了.

代码语言:javascript
运行
复制
muthu@muthu-G31M-ES2L:~/LangFiles/cppfiles/2ndYearLabException$ g++ ExceptionHandlingEdited.cpp
ExceptionHandlingEdited.cpp: In member function ‘void Queue::enqueue(int)’:
ExceptionHandlingEdited.cpp:89:97: warning: deprecated conversion from string constant to ‘char*’
ExceptionHandlingEdited.cpp: In member function ‘int Queue::dequeue()’:
ExceptionHandlingEdited.cpp:113:95: warning: deprecated conversion from string constant to ‘char*’
ExceptionHandlingEdited.cpp: In member function ‘void Queue::view()’:
ExceptionHandlingEdited.cpp:140:66: warning: deprecated conversion from string constant to ‘char*’
ExceptionHandlingEdited.cpp: In function ‘int main()’:
ExceptionHandlingEdited.cpp:185:3: error: expected primary-expression before ‘catch’
ExceptionHandlingEdited.cpp:185:3: error: expected ‘;’ before ‘catch’
ExceptionHandlingEdited.cpp:189:3: error: expected primary-expression before ‘catch’
ExceptionHandlingEdited.cpp:189:3: error: expected ‘;’ before ‘catch’
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-09-27 09:41:58

如果没有try,就不能有catch。放入下面这行:

代码语言:javascript
运行
复制
try {

在您的while语句之前。

如果您希望摆脱有关字符串常量的警告,则可能必须将类型更改为const char *或显式强制转换/复制它们。或者,您可以使用gcc-Wno-write-strings选项。

票数 5
EN

Stack Overflow用户

发布于 2011-09-27 09:47:24

您需要将代码包含在try {...}构造中,否则catch将不知道它应该捕获哪些代码。

while循环包装到try

代码语言:javascript
运行
复制
try {
 while(1)
   {
    .....
   }// end of while(1)
} catch(UnderFlowException e) ...

Reference

票数 4
EN

Stack Overflow用户

发布于 2011-09-27 18:35:09

试试这个:

代码语言:javascript
运行
复制
int main()
{
Queue q;
int choice,data;

 while(1)
 {
  choice  = getUserOption();
  try 
  {
    switch(choice)
    {
     case 1:
       cout<<"\nEnter an element:";
       cin>>data;
       q.enqueue(data);
       break;
     case 2:
       int element;
       element = q.dequeue();
       cout<<"Element Dequeued:\n"<<element;
       break;
     case 3:
       q.view();
       break;
     case 4:
       exit(EXIT_SUCCESS);
    }
  }
  catch(UnderFlowException e)
  {
   cout<<e.display();
  }
  catch(OverFlowException e)
  {
   cout<<e.display();
  }

 }// end of while(1)

       return 0;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7563319

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档