CPP

CPP

Error Handling has 9 Questions

Which keyword is used to handle the expection?

try
throw
catch
none of the mentioned
Which is used to throw a exception?

throw
try
catch
none of the mentioned
What is the use of the 'finally' keyword?

It used to execute at the starting of the program
It will be executed at the end of the program even if the exception arised.
Both a & b
none of the mentioned
What is the output of this program?
  1.     #include 
  2.     using namespace std;
  3.     double division(int a, int b)
  4.     {
  5.         if (b == 0)
  6.         {
  7.             throw "Division by zero condition!";
  8.         }
  9.         return (a / b);
  10.     }
  11.     int main ()
  12.     {
  13.         int x = 50;
  14.         int y = 0;
  15.         double z = 0;
  16.         try 
  17.         {
  18.             z = division(x, y);
  19.             cout << z << endl;
  20.         }
  21.         catch (const char* msg) 
  22.         {
  23.             cerr << msg << endl;
  24.         }
  25.         return 0;
  26.     }

50
0
Division by zero condition!
error
What is the output of this program?
  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     class myexception: public exception
  5.     {
  6.         virtual const char* what() const throw()
  7.         {
  8.             return "My exception";
  9.         }
  10.     } myex;
  11.     int main () 
  12.     {
  13.         try
  14.         {
  15.             throw myex;
  16.         }
  17.         catch (exception& e)
  18.         {
  19.             cout << e.what() << endl;
  20.         }
  21.         return 0;
  22.     }

exception
error
My exception
runtime error
What is the output of this program?
  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     int main () 
  5.     {
  6.         try
  7.         {
  8.             int* myarray = new int[1000];
  9.             cout << "allocated";
  10.         }
  11.         catch (exception& e)
  12.         {
  13.             cout << "Standard exception: " << e.what() << endl;
  14.         }
  15.         return 0;
  16.     }

allocated
Standard exception
Depends on the memory
error
How do define the user-defined exceptions?

inheriting and overriding exception class functionality.
overriding class functioality.
inheriting class functionality
none of the mentioned
Which exception is thrown by dynamic_cast?

bad_cast
bad_typeid
bad_exception
bad_alloc
What is the output of this program?
  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     struct MyException : public exception
  5.     {
  6.         const char * what () const throw ()
  7.         {
  8.             return "C++ Exception";
  9.         }
  10.     };
  11.     int main()
  12.     {
  13.         try
  14.         {
  15.             throw MyException();
  16.         }
  17.         catch(MyException& e)
  18.         {
  19.             cout << "Exception caught" << std::endl;
  20.             cout << e.what() << std::endl;
  21.         }
  22.         catch(std::exception& e)
  23.         {
  24.         }    
  25.     }

C++ Exception
Exception caught
Exception caught C++ Exception
error