Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the need for special exception classes?

Why did the C++ standard bother inventing the std::exception classes? What's their benefit? My reason for asking is this:

try
{
  throw std::string("boom");
}
catch (std::string str)
{
  std::cout << str << std::endl;
}

Works fine. Later, if I need, I can just make my own lightweight "exception" types. So why should I bother with std::exception?

like image 626
Dennis Ritchie Avatar asked Jan 19 '13 13:01

Dennis Ritchie


People also ask

Why do we need custom exception in Java?

Basically, Java custom exceptions are used to customize the exception according to user needs. In simple words, we can say that a User-Defined Exception or custom exception is creating your own exception class and throwing that exception using the 'throw' keyword.

What is the use of exception class in Java?

The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch. The class Exception and any subclasses that are not also subclasses of RuntimeException are checked exceptions.

What are the two main types of exception classes and their purposes?

In terms of Functionality Checked and Unchecked Exception are same. Checked Exception handling verified during compile time while Unchecked Exception is mostly programming errors.

What are the exception classes?

The exception class is a subclass of the Throwable class. Other than the exception class there is another subclass called Error which is derived from the Throwable class. Errors are abnormal conditions that happen in case of severe failures, these are not handled by the Java programs.


1 Answers

Why did the C++ standard bother inventing the std::exception classes? What's their benefit?

It provides a generic and consistent interface to handle exceptions thrown by the standard library. All the exceptions generated by the standard library are inherited from std::exception.

Note that standard library api's can throw a number of different kinds of exceptions, To quote a few examples:

  • std::bad_alloc
  • std::bad_cast
  • std::bad_exception
  • std::bad_typeid
  • std::logic_error
  • std::runtime_error
  • std::bad_weak_ptr | C++11
  • std::bad_function_call | C++11
  • std::ios_base::failure | C++11
  • std::bad_variant_access | C++17

and so on...
std::exception is the base class for all these exceptions:

exceptions hierarchy

providing a base class for all these exceptions, allows you to handle multiple exceptions with a common exception handler.


If I need, I can just make my own lightweight "exception" types. So why should I bother with std::exception?

If you need your custom exception class go ahead and make one. But std::exception makes your job easier because it already provides a lot of functionality which a good exception class should have. It provides you the ease of deriving from it and overidding necessary functions(in particular std::exception::what()) for your class functionality.
This gives you 2 advantages the std::exception handler,

  • can catch standard library exceptions as well as
  • exceptions of the type of your custom exception class

Image courtesy: http://en.cppreference.com/w/cpp/error/exception

like image 100
Alok Save Avatar answered Oct 16 '22 22:10

Alok Save