CPP

CPP

Exceptions That Are Not Errors has 9 Questions
Which is used to handle the exceptions in c++?

catch handler
handler
exception handler
none of the mentioned
Which type of program is recommended to include in try block?

static memory allocation
dynamic memory allocation
const reference
pointer
Which statement is used to catch all types of exceptions?

catch()
catch(Test t)
catch(...)
none of the mentioned
What is the output of this program?
  1.     #include 
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int x = -1;
  6.         try 
  7.         {
  8.             if (x < 0)
  9.             {
  10.                 throw x;
  11.             }
  12.             else
  13.             {
  14.                 cout<<x;
  15.             }
  16.         }
  17.         catch (int x )
  18.         {
  19.             cout << "Exception occurred: Thrown value is " << x << endl;
  20.         }
  21.         return 0;
  22.     }

-1
0
Exception occurred: Thrown value is -1
error
What is the output of this program?
  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     void myunexpected () 
  5.     {
  6.         cout << "unexpected handler called\n";
  7.         throw;
  8.     }
  9.     void myfunction () throw (int,bad_exception) 
  10.     {
  11.         throw 'x';
  12.     }
  13.     int main (void)
  14.     {
  15.         set_unexpected (myunexpected);
  16.         try 
  17.         {
  18.             myfunction();
  19.         }    
  20.         catch (int) 
  21.         { 
  22.             cout << "caught int\n"; 
  23.         }
  24.         catch (bad_exception be) 
  25.         { 
  26.             cout << "caught bad_exception\n"; 
  27.         }
  28.         catch (...) 
  29.         { 
  30.             cout << "caught other exception \n"; 
  31.         }
  32.         return 0;
  33.     }

unexpected handler called
caught bad_exception
caught other exception
both a & b
What is the output of this program?
  1.     #include 
  2.     using namespace std; 
  3.     int main()
  4.     {
  5.         int x = -1;
  6.         char *ptr;
  7.         ptr = new char[256];
  8.         try 
  9.         {
  10.             if (x < 0)
  11.             {
  12.                 throw x;
  13.             }
  14.             if (ptr == NULL)
  15.             {
  16.                 throw " ptr is NULL ";
  17.             }
  18.         }
  19.         catch (...) 
  20.         {
  21.             cout << "Exception occurred: exiting "<< endl;
  22.         }
  23.         return 0;
  24.     }

-1
ptr is NULL
Exception occured: exiting
none of the mentioned
What is the output of this program?
  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     void myunexpected ()
  5.     {
  6.         cout << "unexpected called\n";
  7.         throw 0;
  8.     }
  9.     void myfunction () throw (int) 
  10.     {
  11.         throw 'x';
  12.     }
  13.     int main () 
  14.     {
  15.         set_unexpected (myunexpected);
  16.         try 
  17.         {
  18.             myfunction();
  19.         }
  20.         catch (int) 
  21.         {
  22.             cout << "caught int\n";
  23.         }
  24.         catch (...)  
  25.         { 
  26.             cout << "caught other exception\n"; 
  27.         }
  28.         return 0;
  29.     }

caught other exception
caught int
unexpected called
both b & c
How to handle error in the destructor?

throwing
terminate
both a & b
none of the mentioned
What kind of exceptions are available in c++?

handled
unhandled
static
dynamic