#G090. GESP level4 异常处理
GESP level4 异常处理
GESP level4 - 异常处理
- 下面的程序中,如果输入10 0 ,会输出( )
{{ select(1) }}1 #include 2 using namespace std; 3 double Division(int a,int b) { 4 if(b==0) 5 throw "Division by zero condition!"; 6 else 7 return (double)a/(double)b; 8 } 9 void func() 10 { 11 try 12 { 13 int len, time; 14 cin>>len>>time 15 cout<<Division(len,time)<<endl; 16 } 17 catch (...) 18 { 19 cout << "error." << endl; return; 20 } 21 } 22 int main() 23 { 24 func(); 25 return 0; 26 }
- Division by zero condition!
- 0
- 10
- 100
- 运行下面的代码,将出现( )
{{ select(2) }}double hmean(double a, double b) { if (a == -b ) throw runtime_error("Runtime error occurred."); return 2.0ab/(a + b); } int main() { double x = 10; double y = -10; try { int result = hmean(x, y); cout << "hmean: " << result << endl; } catch (const runtime_error& e) { cout << "Caught: " << e.what() << endl; } catch (...) { cout << "Caught an unknown exception." << endl; } return 0; }
- 屏幕上输出Caught: Runtime error occurred.
- 屏幕上输出Caught an unknown exception.
- 程序调用 std::terminate()
- 编译错误
- 运行下面的代码,将出现什么情况?( )
{{ select(3) }}double hmean(double a, double b) { if (a == -b ) throw runtime_error("Runtime error occurred"); return 2.0ab/(a + b); } int main() { double x = 10; double y = -10; try { int result = hmean(x, y); cout << "hmean: " << result << endl; } catch (const runtime_error& e) { cout << "Caught: " << e.what() << endl; } catch (...) { cout << "Caught an unknown exception." << endl; } return 0; }
- 屏幕上输出Caught: Runtime error occurred
- 屏幕上输出Caught an unknown exception
- 程序调用 std::terminate()
- 编译错误
- 下列关于C++语言中异常处理的叙述,正确的是( )。 {{ select(4) }}
- 一个try子句可以有多个catch子句与之对应
- 如果try子句在执行时发生异常,就一定会进入某一个catch子句执行
- 如果try子句中没有可能发生异常的语句,会产生编译错误
- catch 子句处理异常后,会重新执行与之对应的try子句
- 在C++语言中,如果一个函数可能抛出异常,那么一定要在try子句里调用这个函数。( ) {{ select(5) }}
- 对
- 错
- 在C++语言中try子句里抛出的结构体等类型的异常无法被catch捕获。( ) {{ select(6) }}
- 对
- 错
- 在下列代码的横线处填写( ),可以使得输出是“20 10”
{{ select(7) }}1 #include 2 using namespace std; 3 void xchg(__________) { 4 int t=*x; 5 *x=*y; 6 *y =t; 7 } 8 int main(){ 9 inta=10,b=20; 10 xchg(&a,&b); 11 cout<<a<<" "<<b<<endl; 12 return 0; 13 }
- int x, int y
- int * x, int * y
- int a, int b
- int & a, int & b
- 在C++语言中,如果一个函数可能抛出异常,那么一定要在try子句里调用这个函数。( ) {{ select(8) }}
- 对
- 错
- 运行下面的代码,屏幕上将输出( )
{{ select(9) }}1 #include 2 using namespace std; 3 int divide(int a, int b) { 4 if (b == 0) { 5 throw runtime_error("division by zero error"); 6 } 7 return a / b; 8 } 9 int main() { 10 int x = 10; 11 int y = 0; // 设为 0 会导致除零错误 12 try { 13 int result = divide(x, y); 14 cout << "result: " << result << endl; 15 } 16 catch (const runtime_error& e) { 17 cout << "caught an exception: " << e.what() << endl; 18 } 19 return 0; 20 }
- division by zero error result: caught an exception:
- result: caught an exception: division by zero error
- caught an exception: division by zero error
- division by zero error caught an exception: division by zero error