c++ - Unicode exception class -


i want implement header-only class unicode exception , i've started following code:

#pragma once  #include <string> #include <exception>  using namespace std;  class uexception : public exception { public:     explicit uexception(const wchar_t* msg)     {          this->msg = msg;     }      explicit uexception(const wstring& msg)     {          this->msg = msg;     }      const wchar_t* uwhat() const throw ()     {         return msg.c_str();     }  private:     wstring msg;      const char* what() const throw () //hidden     {          return null;       }  }; 

this works fine, have questions:

  1. why need derive std::exception class? may not needed @ all?
  2. am miss in class implementation?

  1. you don't have inherit std::exception. inheriting directly, or indirectly has advantage of allowing users of interface catch exception reference std::exception - without depending on definition of exception type.

    of course, accessing uwhat won't possible through std::exception anyway, if don't intend implement suggestion, not inheriting std::exception might desirable design.

  2. if decide inherit, reducing visibility of what not possible. ††

    your implementation of what violates interface of std::exception not returning pointer null terminated string. if exception handler catches reference std::exception, calls what() , dereferences pointer, chaos ensue.

    a trivial way implement what return "" instead. however...

recommend considering alternative: instead of returning "nothing", convert encoding of stored wide string native narrow string enconding , return pointer that. sure, lot more work implement, make exception more convenient.

†† technically can reduce visibility of uexception::what, have not affect visibility of uexception::exception::what. besides, doing violates liskov substitution principle. recommend either inherit std::exception , implement what publicly , properly, or don't inherit std::exception @ all.


Comments

Popular posts from this blog

matlab - error with cyclic autocorrelation function -

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

c# - What is a good .Net RefEdit control to use with ExcelDna? -