一、判断题
1、If you are not interested in the contents of an exception object, the catch block parameter may be omitted.。
T F
解析:T。在某些情况下,当捕获异常时,您可能对异常对象内部的具体信息不感兴趣。如果您只关心发生了异常的事实,而不需要访问或使用异常对象本身,您可以省略 catch 块的参数。
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 F
解析:T。在使用特定类型捕获异常时,catch
参数(在这种情况下是 p
)实际上就像函数参数一样。它允许您在 catch
块的主体内访问与抛出的异常相关联的值或信息。
3、异常处理的catch{ }语句块必须紧跟try{ }语句块之后,这两个语句之间不能插入另外语句。
T F
解析:T。异常处理的 catch
块必须紧跟在 try
块之后,但是在它们之间不可以插入其他语句。
4、有如下语句序列
第1行: int a=1;
第2行: try{
第3行: if(a==1) throw(a);
第4行: a++;
第5行: }
第6行: catch(int b){
第7行: cout << “error! a = ” << b << endl;
第8行: }
以上语句的第6行有编译错误,只能写成catch(int)。
T F
解析:F。catch
块可以使用一个参数来捕获异常,也可以不使用参数,只写 catch
,这将捕获任何类型的异常,故并不是只能写成catch(int)。
二、单选题
1、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
解析:D。C++ 中的一个主要特性是异常处理,它是更好的错误处理方式。
2、What is wrong in the following code?
vector<int> 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].
解析:C。在访问v[0]时发生错误ÿ