dimanche 13 mai 2018

What is the best way to handle many custom exceptions

Due to some requirements, in my cpp library I need to add many custom exceptions(Almost 50+), so I am thinking to write a custom exceptions like below,

 #include <iostream>
 #include <exception>

 using namespace std;

 class ScaleException: public exception
 {
   virtual const char* what() const throw()
   {
     return "My ScaleException happened";
   }
 };



 class NotExistException: public exception
 {
   virtual const char* what() const throw()
   {
     return "NotExistException";
   }
 };

 class StateException: public exception
 {
   virtual const char* what() const throw()
   {
     return "StateException";
   }
 };


 int main ()
 {


   try
   {
     throw ScaleException();
   }
   catch (exception& e)
   {
     cout << e.what() << endl;
   }
   return 0;
 }

But my worry is that I need to write so many custom exceptions class (I have almost 50+ different kind of exceptions, so I may endup writing those many exception class) , is there any way to define all in one or few classes, and it would easy and meaning full to throw the exceptions.

What kind of design I should have?

Aucun commentaire:

Enregistrer un commentaire