PTA-C++(异常处理2)

一.判断题

1.If you are not interested in the contents of an exception object, the catch block parameter may be omitted.。(T)

2.catch (type p) acts very much like a parameter in a function. Once the exception is caught, you can access the thrown value from this parameter in the body of a catch block.。(T)

二.单选题

2-1
Suppose Exception2 is derived from Exception1. Analyze the following code.(A)

try {

statement1;

statement2;

statement3;
}

catch (Exception1 ex1)
{
}

catch (Exception2 ex2)
{
}

A.If an exception of the Exeception2 type occurs, this exception is caught by the first catch block.
B.If an exception of the Exeception2 type occurs, this exception is caught by the second catch block.
C.The program has a compile error because these two catch blocks are in wrong order.
D.The program has a runtime error because these two catch blocks are in wrong order.

2-2
Suppose that statement2 throws an exception of type Exception2 in the following statement:(D)

try {

statement1;

statement2;

statement3;
}

catch (Exception1 ex1)
{
}

catch (Exception2 ex2)
{
}

catch (Exception3 ex3)
{
statement4;
throw;
}

statement5;

A.statement2
B.statement3
C.statement4
D.statement5

2-3
Suppose that statement3 throws an exception of type Exception3 in the following statement:

try {

statement1;

statement2;

statement3;

}

catch (Exception1 ex1)
{
}

catch (Exception2 ex2)
{
}

catch (Exception3 ex3)
{
statement4;
throw;
}

statement5;

Which statements are executed after statement3 is executed?( C)

A.statement2
B.statement3
C.statement4
D.statement5

2-4
下列关于异常的描述中,错误的是(A)。

A.编译错属于异常,可以抛出
B.运行错属于异常
C.硬件故障也可当异常抛出
D.只要是编程者认为是异常的都可当异常抛出

2-5
下列关于异常类的说法中,错误的是。(A)

A.异常类由标准库提供,不可以自定义
B.C++的异常处理机制具有为抛出异常前构造的所有局部对象自动调用析构函数的能力
C.若catch块采用异常类对象接收异常信息,则在抛出异常时将通过拷贝构造函数进行对象复制,异常处理完后才将两个异常对象进行析构,释放资源
D.异常类对象抛出后,catch块会用类对象引用接收它以便执行相应的处理动作

2-6
C++处理异常的机制是由(B)3部分组成。

A.编辑、编译和运行
B.检查、抛出和捕获
C.编辑、编译和捕获
D.检查、抛出和运行

2-11
One of the major features in C++ is (D ) handling,which is a better way of handling errors.

A.data
B.pointer
C.test
D.exception

2-12
What is wrong in the following code?( C)

vector v;
v[0] = 2.5;

A.The program has a compile error because there are no elements in the vector.
B.The program has a compile error because you cannot assign a double value to v[0].
C.The program has a runtime error because there are no elements in the vector.
D.The program has a runtime error because you cannot assign a double value to v[0].

2-13
If you enter 1 0, what is the output of the following code?(D)

#include “iostream”
using namespace std;

int main()

{
// Read two integers

cout << "Enter two integers: ";

int number1, number2;

cin >> number1 >> number2;

try
{
if (number2 == 0)
throw number1;

cout << number1 << " / " << number2 << " is "
  << (number1 / number2) << endl;

cout << "C" << endl;

}
catch (int e)
{
cout << “A” ;
}

cout << “B” << endl;

return 0;
}

A.A
B.B
C.C
D.AB

2-14
The function what() is defined in A______.

A.exception
B.runtime_error
C.overflow_error
D.bad_exception

三.函数题

6-1 求两个数的调和平均数(异常处理)

分数 6
作者 张德慧
单位 西安邮电大学
两个数a和b的调和平均数的计算公式为:2×a×b/(a+b),下列函数hmean计算a和b的调和平均数并进行异常处理。请完成该函数并提交。

函数接口定义:
double hmean(double a, double b) throw(const char );
在这里解释接口参数。例如:其中 a 和 b 都是用户传入的参数。函数须返回 a 和 b 的调和平均数。
裁判测试程序样例:
#include
using namespace std;
/
你提交的代码将嵌入到这里 */

int main()
{
double x, y;
cin >> x >> y;
try {
cout<<hmean(x,y);
}
catch (const char * s)
{
cout << s << endl;
}
return 0;
}
输入样例:
2 -2
输出样例:
bad hmean() arguments: a = -b not allowed

 double hmean(double a, double b) {
    if(a+b==0){
        throw("bad hmean() arguments: a = -b not allowed");
    }
    return (2*a*b)/(a+b);
 }

6-15 除数为零异常

分数 10
作者 张德慧
单位 西安邮电大学
下面是这个程序处理除数为零的异常,在函数 division( )中抛出一个除以零的异常,并在main函数中的 catch 块中捕获该异常。

函数接口定义:
double division(int a, int b);
其中a为被除数,b为除数。

裁判测试程序样例:
#include
using namespace std;

/* 请在这里填写答案 */

int main ()
{
int x,y;
double z = 0;

cin>>x>>y;
try {
    z = division(x, y);
    cout << z << endl;
}catch (const char* msg) {
    cout << msg << endl;
}
return 0;

}
输入样例:
在这里给出一组输入。例如:

2 0
输出样例:
在这里给出相应的输出。例如:

Divided by zero!

double division(int a, int b){
    if(b==0){
        throw("Divided by zero!");
    }
    return a/b;
}

四.编程题

7-1 数字格式异常

分数 10
作者 张德慧
单位 西安邮电大学
(NumberFormatException数字格式异常)编写一个程序,提示用户读取两个整数,然后显示他们的和。程序应该在输入不正确时提示用户再次输入数字。

输入格式:
i 9 (第1次输入)
l 8 (第2次输入)
5 6 (第3次输入)

输出格式:
Incorrect input and re-enter two integers: (第1次输出提示)
Incorrect input and re-enter two integers: (第2次输出提示)
Sum is 11 (输出结果)

输入样例:
i 9
l 8
5 6
输出样例:
Incorrect input and re-enter two integers:
Incorrect input and re-enter two integers:
Sum is 11

暂无

7-16 求平方根函数mySqrt的异常处理

分数 10 作者 张德慧 单位 西安邮电大学 改造下面的求平方根函数mySqrt,当x小于0时,输出错误信息:“Error: Can not take sqrt of negative number”;当x不小于0时,输出x的平方根。要求在main函数中采用C++的异常处理方法。

double mySqrt(double x)
{
return sqrt(x);
}

输入格式:
4

输出格式:
The sqrt of 4 is 2

输入样例:
-9
输出样例:
Error: Can not take sqrt of negative number

if-else语句实现

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

double mySqrt(double x)
{ 
    if (x>=0)
        return sqrt(x); 
    else 
        return -1;
}
int main()
{
    double x,y;
    cin>>x;
    y = mySqrt(x);
    if (y==-1)
        cout<<"Error: Can not take sqrt of negative number";
    else
        cout<<"The sqrt of "<<x<<" is "<<y;
    return 0;
}

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值