Exception Objects in C++

Estudies4you
Explain Exception Objects in C++

 EXCEPTION OBJECTS 

Q16. Discuss about exception objects. 
Answer: 
Exception Objects 
The exception object holds the error information about the exception that had occurred. The information includes the type of error i.e., logic error or run time error and state of the program when the error occurred. An exception object is created as soon as exception occurs and it is passed to the corresponding catch block as a parameter. This catch block contains the code to catch the occurred exception. It uses the methods of exception object for handling the exceptions. 
Syntax 

try 

{

throw exception object; 

}

catch (Exception &exceptionobject) 

{

}

To retrieve the message attached to the exception, exception object uses a method called what (). This method returns the actual messages.


Syntax for what () Method
exceptionobject.what();

Program 

#include <iostream> 

#include <exception> 

using namespace std; 

class MyException : public exception 

{

public: 

const char * what() const throw() 

{

return" divide by zero exceptionl\n"; 

}

}; 

int main() 

{

try 

{

int x, y; 

cout << "Enter the two numbers :. \n"; 

cin>>x>>y; 

if(y==0) 

{

MyException z; 

throw z;

}

else 

{

cout <<"x/y =" <<x/y << endl; 

}

}

catch(exception &e) 

{

cout<<e.what();

}

}


Output
examples of Exception Objects in C++,explain about Exception Objects in C++,notes on Exception Objects in C++,Exception Objects lecture notes


To Top