第十二章 异常处理

判断题

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 __.(D)
// 搞不懂
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块会用类对象引用接收它以便执行相应的处理动作

7.Suppose Exception2 is derived from Exception1. Analyze the following code.
//假设异常2派生自异常1。分析以下代码。
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.

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

9.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)
//在语句3执行之后执行哪些语句?
A.statement2
B.statement3
C.statement4
D.statement5

10.C++处理异常的机制是由()3部分组成。(B)
A.编辑、编译和运行
B.检查、抛出和捕获
C.编辑、编译和捕获
D.检查、抛出和运行

函数题

1.除数为零异常
下面是这个程序处理除数为零的异常,在函数 division( )中抛出一个除以零的异常,并在main函数中的 catch 块中捕获该异常。

函数接口定义:

double division(int a, int b);

其中a为被除数,b为除数。
裁判测试程序样例:

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

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

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

2.数字格式异常
(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<iostream>

using namespace std;

int main()
{
	char a,b;
	cin>>a>>b;
	while(1)
	{
		try{
			if(a<'0'||a>'9')
				throw(0);
			if(a<'0'||a>'9')
				throw(0);
			cout<<"Sum is "<<int(a)+int(b)-96<<endl;
			return 0;
			
		}
		catch(int)
		{
			cout<<"Incorrect input and re-enter two integers:\n";
			cin>>a>>b;
		}
	}

}
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很高兴为您提供 Python 第十一章上机实践。本章的实践内容主要是关于文件处理、异常处理和模块的使用。 1. 文件处理 第一道题目是要求读取一个文件并统计其单词的数量。您可以按照以下步骤完成任务: (1)使用 open() 函数打开文件。 (2)使用 read() 函数读取文件内容。 (3)使用 split() 函数将文件内容按照空格分割成单词。 (4)使用 len() 函数统计单词数量。 下面一个示例代码: ```python with open('file.txt', 'r') as f: content = f.read() words = content.split(' ') count = len(words) print(f'The file has {count} words.') ``` 2. 异常处理 第二道题目是要求编写一个函数,将两个数相除并返回结果。但如果除数为 0,则应该抛出一个 ZeroDivisionError 异常。您可以按照以下步骤完成任务: (1)定义一个函数。 (2)使用 if 语句判断除数是否为 0。 (3)如果除数为 0,则使用 raise 语句抛出一个 ZeroDivisionError 异常。 (4)如果除数不为 0,则返回两个数的商。 下面一个示例代码: ```python def divide(num1, num2): if num2 == 0: raise ZeroDivisionError('division by zero') else: return num1 / num2 try: result = divide(10, 0) except ZeroDivisionError as e: print(e) ``` 3. 模块的使用 第三道题目是要求使用 Python 标准库的 random 模块生成一个随机数。您可以按照以下步骤完成任务: (1)使用 import 语句导入 random 模块。 (2)使用 random 模块的 randint() 函数生成一个随机数。 下面一个示例代码: ```python import random num = random.randint(1, 100) print(f'The random number is {num}.') ``` 希望这些实践题可以帮助您更好地理解 Python 文件处理、异常处理和模块的使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值