c++ - C++中的异常处理

标签 c++

我有以下使用异常处理的程序。

#include<iostream.h>
#include<conio.h>

void divide(int x,int y,int z)
{
  cout<<"\n Inside function 1 \n";

  if((x-y)!=0)
  {
    int R=z/(x-y);
    cout << "Result =" << R<<"\n";
  } // end of if
  else
  {
    throw(x-y);
  }  // end of else
}   // end of void

int main()
{
  try
  {
    cout<< "Inside try block\n";
    divide(10,20,30);
    divide(10,10,20);
  } //end of try
  catch(int i)
  {
    cout<< "Caught\n";
  }
  return 0;
} //end of main

编译时出现如下错误

Function throw should have a prototype.

Undefined symbol "try"

Statement missing ;

Function should return a value.

请帮助我,非常感谢

最佳答案

用一些非古老的 ​​C++ 编译器编译,使用 #include <iostream>而不是 #include <iostream.h> , 然后放下 #include <conio.h> , 那么一旦你有了 using std::cout; 它就应该工作了

关于c++ - C++中的异常处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3644976/

相关文章:

c++ - 为什么 GET_X_LPARAM 确实返回鼠标滚轮上的绝对位置?

c++ - CMake configure_file 未包含在静态库中

c++ - 读取 MIDI 文件

c++ - 如何从 clang::ASTContext 生成代码

c++ - 使用一个实现其他类方法的类方法以及它自己的类方法

c++ - 连接到 Firebird C++ 时出错

C++ libserial 跳过 0x09

c++ - 使用指针从列表中删除元素

c++ - 当值部分不存在时使用 Boost Property Tree 读取 INI 文件

c++ - 基于 malloc/free 的 STL 分配器