一道简单的sicily题(关于错误处理的),但就是wrong answer
一道简单的sicily题(关于异常处理的),但就是wrong answer
本帖最后由 HJW00000 于 2012-06-14 22:10:31 编辑 某小朋友已经四岁了,她开始学习数字减法,但她还不理解负数的概念,如果被减数和减数中出现负数,或者被减数小于减数,她都不会算。
函数calc做减法计算前进行了检查。
int calc(int a, int b) throw(logic_error)
{
if (a<0) throw out_of_range("Out of range exeception");
else if (b<0) throw out_of_range("Out of range exeception");
else if (a<b) throw logic_error("Minuend smaller than subtrahend");
return a-b;
}
请写一个test函数
void test(int, int)
使得以下函数f
void f()
{
test(-3,1);
test(1,-3);
test(1,3);
}
输出如下:
Out of range exeception
Out of range exeception
Minuend smaller than subtrahend
请只提交test函数,不要提交calc函数。
我的代码:
void test(int a, int b)
{
int t;
try
{
t=calc(a,b);
}
catch(exception ex)
{
cout<<ex.what()<<endl;
}
}
可以告诉我为什么错吗?
------解决方案--------------------
已经解决了,是题目本身没说清楚要求。
本帖最后由 HJW00000 于 2012-06-14 22:10:31 编辑 某小朋友已经四岁了,她开始学习数字减法,但她还不理解负数的概念,如果被减数和减数中出现负数,或者被减数小于减数,她都不会算。
函数calc做减法计算前进行了检查。
int calc(int a, int b) throw(logic_error)
{
if (a<0) throw out_of_range("Out of range exeception");
else if (b<0) throw out_of_range("Out of range exeception");
else if (a<b) throw logic_error("Minuend smaller than subtrahend");
return a-b;
}
请写一个test函数
void test(int, int)
使得以下函数f
void f()
{
test(-3,1);
test(1,-3);
test(1,3);
}
输出如下:
Out of range exeception
Out of range exeception
Minuend smaller than subtrahend
请只提交test函数,不要提交calc函数。
我的代码:
void test(int a, int b)
{
int t;
try
{
t=calc(a,b);
}
catch(exception ex)
{
cout<<ex.what()<<endl;
}
}
可以告诉我为什么错吗?
------解决方案--------------------
已经解决了,是题目本身没说清楚要求。