简体   繁体   English

除以零例外

[英]divide by zero exception

Can anyone help me about this? 有人可以帮我吗? The compiler doesn't recognize the exception e but how I should declare it? 编译器无法识别异常e,但是我应该如何声明呢? The application simply should divide two numbers throwing an exception if the second is 0. 如果第二个数字为0,则应用程序只需将两个数字相除并引发异常。

#include <iostream>
#include <exception>

using namespace std;

float divide(float, float) throw(exception);

int main(int argc, char** argv) {

  float a,b;

  float c=0;
  cin>>a;
  cin>>b;
  try{
    c=divide(a,b);
  }
  catch(Exception e){
    cout<<"You cannot do this";
  };
  return 0;
}

float divide(float a, float b) throw(exception)  {
  float c=a/b;
  if(b==0)
    throw  Exception &e;
  return c;
}

Well, you tried to use a class Exception but never declared it. 好吧,您尝试使用Exception类,但从未声明过。 So it does not exist. 因此它不存在。

You could use one of the standard exception types. 您可以使用标准异常类型之一。 In fact, it looks like you may be attempting to use the base class std::exception : that's a lower-case e . 实际上,您似乎正在尝试使用基类std::exception :这是小写的 e

I suggest std::runtime_error from <stdexcept> , instead. 我建议使用<stdexcept> std::runtime_error

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM