![]() |
VOOZH | about |
In C++, exceptions are not limited to built-in data types. We can also define our own exception types using classes and throw objects of those classes. This allows developers to represent application-specific error conditions in a structured and type-safe way.
A user-defined exception is thrown using:
throw ClassName();
Example 1: Exception Handling with a Single Class
Caught exception of demo class
Explanation:
Example 2: Exception Handling with Multiple Classes
Caught exception of demo1 class Caught exception of demo2 class
When exception classes are related through inheritance, the order of catch blocks becomes important.
Caught exception of demo1 class Caught exception of demo1 class
Explanation:
Exceptions can also be thrown and handled inside constructors.
Exception caught Zero not allowed Again creating object Num = 1
Explanation: