15 异常处理【C++】

15 异常处理【C++】,第1张

15 异常处理
  • 15-
    • 判断题
    • 单选题
    • 函数题
      • 1.除数为零异常
    • 编程题
      • 1.求平方根函数mySqrt的异常处理
  • 15+
    • 判断题
    • 单选题
    • 函数题
      • 6-1 求两个数的调和平均数(异常处理)
      • 6-15 除数为零异常
    • 编程题
      • 7-1 数字格式异常
      • 7-2.求平方根函数mySqrt的异常处理

15- 判断题

1.If you are not interested in the contents of an exception object, the catch block parameter may be omitted.(T)
如果您对异常对象的内容不感兴趣,可以省略catch block参数。

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)
catch(类型p)的作用非常类似于函数中的参数。一旦捕获到异常,就可以从catch块体中的此参数访问抛出的值

单选题

1.One of the major features in C++ is ( ) handling,which is a better way of handling errors.(D)
A.data
B.pointer
C.test
D.exception

2.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].

3.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

4.The function what() is defined in __.(A)
A. exception
B. runtime_error
C. overflow_error
D. bad_exception

5.下列关于异常的描述中,错误的是()。(A)
A.编译错属于异常,可以抛出
B.运行错属于异常
C.硬件故障也可当异常抛出
D.只要是编程者认为是异常的都可当异常抛出

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

函数题 1.除数为零异常
1.除数为零异常
下面是这个程序处理除数为零的异常,在函数 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;
}
编程题 1.求平方根函数mySqrt的异常处理
1.求平方根函数mySqrt的异常处理
改造下面的求平方根函数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
#include
#include
using namespace std;

double mySqrt(double x)
{ 
	if(x<0)
		throw("Error: Can not take sqrt of negative number");
	return sqrt(x);
 }

int main()
{
	double n;
    double r=0;
	cin>>n;
	try
	{
		r=mySqrt(n);
        cout<<"The sqrt of "<<n<<" is "<<r;
	}
	catch(const char *arg)
	{
		cout<<arg;
		
	}
	
	return 0;
}
15+ 判断题

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

1-2 T
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.。

单选题

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

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 D
Suppose that statement2 throws an exception of type Exception2 in the following statement:

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 C
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?
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 B
C++处理异常的机制是由()3部分组成。
A.编辑、编译和运行
B.检查、抛出和捕获
C.编辑、编译和捕获
D.检查、抛出和运行

2-11 D
One of the major features in C++ is ( ) handling,which is a better way of handling errors.
A.data
B.pointer
C.test
D.exception

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

  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 D
If you enter 1 0, what is the output of the following code?

#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 A
The function what() is defined in ______________.
A.exception
B.runtime_error
C.overflow_error
D.bad_exception

函数题 6-1 求两个数的调和平均数(异常处理)
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){
    
    double c=b*-1;
    if(c==a){
        throw("bad hmean() arguments: a = -b not allowed");
    }else{
         return 2*a*b/(a+b);
    }
    
    
}
6-15 除数为零异常
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 数字格式异常
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
#include 
#include
using namespace std;
int f;
int toNumb(char co[])
{
    int len=strlen(co);
    int i;
    int sum=0;
    int w=1;
    for(i=len-1;i>=0;i--)
    {
        if(!(co[i]<='9' && co[i]>='0'))
        {
            f=1;
            return 0;
        }
        sum+=w*(co[i]-'0');
        w*=10;
    }
    return sum;
}
int main ()
{
    string a,b;
    while(1)
    {
        cin>>a>>b;
        f=0;
        int m=toNumb(&a[0]);
        int n=toNumb(&b[0]);
        if(f)
        {
            cout<<"Incorrect input and re-enter two integers:"<<endl;
            continue;
        }
        else
        {
            cout<<"Sum is "<<m+n<<endl;
            break;
        }
    }

} 
7-2.求平方根函数mySqrt的异常处理
7-2.求平方根函数mySqrt的异常处理
改造下面的求平方根函数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
#include
#include
using namespace std;

double mySqrt(double x)
{ 
	if(x<0)
		throw("Error: Can not take sqrt of negative number");
	return sqrt(x);
 }

int main()
{
	double n;
    double r=0;
	cin>>n;
	try
	{
		r=mySqrt(n);
        cout<<"The sqrt of "<<n<<" is "<<r;
	}
	catch(const char *arg)
	{
		cout<<arg;
		
	}
	
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1331056.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-12
下一篇 2022-06-12

发表评论

登录后才能评论

评论列表(0条)

保存