+ -
当前位置:首页 → 问答吧 → 异常

异常

时间:2011-11-29

来源:互联网

C/C++ code

#include<iostream>
#include<exception>
using namespace std;

void myunexcepted()
{
    throw std::bad_exception();//默认构造函数建一个bad_exception 对象
}

int d(int,int) throw(const char*);

//set_unexpected(myunexcepted);

int main()
{
    int x,y,z;
    cout<<"please inter two numbers:";
    cin>>x;
        cin>>y;
    try
    {
        z=d(x,y);
    }
    catch (const char *s)
    {
        cout<<s<<endl;
        
    }
    catch(bad_exception)
    {
        cout<<"有一个意外异常\n";
    }
    return 0;
}

int d(int x,int y)
{
    if(y=0)
    {throw "除数不能为零";}//  bug?怎么这一句跟y被赋值了一样
    
    return x/y;
}



  用的是vc6.0,同时vs2010也试了一下,set_unexpected这个函数一个是提醒函数调用不对,2010里包含头文件以后提示没有这个函数,请问为什么
  另外就是下边注释的那一句,怎么执行完那一句,y就变为0了呢?

作者: zxjluohe   发布时间: 2011-11-29

自己顶

作者: zxjluohe   发布时间: 2011-11-29

都习惯了自己顶了,没回都是,难道是我不会起名?

作者: zxjluohe   发布时间: 2011-11-29

if(y=0)
你确信你要赋值?

作者: qscool1987   发布时间: 2011-11-29

if(y=0) 这一句确实是y被赋值了

作者: mougaidong   发布时间: 2011-11-29

引用楼主 zxjluohe 的回复:
if(y=0)
{throw "除数不能为零";}// bug?怎么这一句跟y被赋值了一样


楼主的注释很可爱

作者: mougaidong   发布时间: 2011-11-29