PART III: Structuring of Computations- Structuring the computation, Expressions and statements, Conditional execution and iteration, Routines, Style issues: side effects and aliasing, Exceptions, Case Study-Exception handling in C++. TIO An exception is thrown using keyword in throws throw threw thrown B try
TIO catch finally A Exception handlers are declared with try catch finally B 2
TIO Catch handler can have multiple parameters. True False TIO A Which of the following statements are true about Catch \ 3
handler?. It must be placed immediately after try block T 2. It can have multiple parameters 3. There must be only one catch handler for every try block 4. There can be multiple catch handler for a try block T 5. Generic catch handler can be placed anywhere after try block. TIO Only, 4, 5 Only, 2, 3 Only, 4 Only, 2 C A try block can be nested under another try block Yes 4
No TIO A In nested try blocks, there is no need to specify catch handler for inner try block. Outer catch handler is sufficient for the program. a. True b. False True False 5
TIO B For every try block, there must be at least one catch hander. Otherwise, compiler will generate error. TIO In nested try block, if inner catch handler gets executed, then Program execution stops immediately Outer catch handler will also get executed Compiler will jump to the outer catch handler and then executes remaining executable statements of main( Compiler will execute remaining executable statements of outer try block and then the main( D Compiler will execute remaining executable statements of outer try block and then the main( 6
TIO If inner catch handler is not able to handle the exception then Compiler will look for outer try handler Program terminates abnormally Compiler will check for appropriate catch handler of outer try block of these C Compiler will check for appropriate catch handler of outer try block Generic catch handler must be placed at the end of all the Catch handlers. Tr True False 7
TIO A TIO Generic catch handler is represented by catch(.., catch(--- catch( catch( void x C 8
In nested try blocks, if both inner and outer catch handlers are not able to handle the exception, then TIO Compiler executes only executable statements of main( Compiler issues compile time errors about it Program will be executed without any interrupt Program will be terminated abnormally D Functions called from within a try block may also throw 9
True False TIO A Irrespective of exception occurrence, catch handler will always get executed. True False B 0
TIO Catch handler will only get execute when exception occurs at runtime Throwing an unhandled exception causes standard library function to be invoked. TIO stop( aborted( terminate( abandon( C An exception can be of only built-in type.
True False TIO B An exception can be of any type, including class type that we create. How can we restrict a function to throw certain exceptions? Defining multiple try and catch block inside a function Defining generic function within try block Defining function with throw clause It is not possible in C++ to restrict a function C 2
TIO TIO Attempting to throw an exception that is not supported by a function call results in calling library function. indeterminate( unutilized( unexpected( unpredicted( C 3
TIO Return type of uncaught_exception( is int bool char * double B Can we write throw clause inside catch handler? Yes No 4
TIO A Can we define our exceptions in CPP? Yes No TIO A 5
What is the output of this program? #include <iostream> using namespace std; int main( { int x = -; try { if (x < 0 { throw x; } else { cout<<x; } } catch (int x { cout << "Exception occurred: Thrown value is " << x << endl; } return 0; } - 0 Exception Occurred: Thrown value is - error 6
TIO C As the given value is - and according to the condition, We are arising an exception. Refer more questions on exception handling in C++ of last OOP MCQs 7