LCOV - code coverage report
Current view: top level - src/bindings/cpp/examples - cpp_example_userexception.cpp (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 0 17 0.0 %
Date: 2019-09-12 12:28:41 Functions: 0 5 0.0 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief This example explains how to define user defined exception.
       5             :  *
       6             :  * That means that the user can implement their own exceptions,
       7             :  * e.g. potentially derived from their own base class with its
       8             :  * own what() information.
       9             :  * The user could even decide to use another mechanism
      10             :  * instead of what().
      11             :  *
      12             :  * The only requirements are: They must be named exactly like
      13             :  * the original classes and KDBException constructor must take a key
      14             :  * as argument (which has all information like error and warnings).
      15             :  *
      16             :  * It does not matter from which class the exceptions are derived
      17             :  * or which members they have (if they are binary compatible) as
      18             :  * long as they are used everywhere.
      19             :  *
      20             :  * Never use non-binary compatible user exceptions if you do not
      21             :  * use them everywhere where you include any of elektras header
      22             :  * files!
      23             :  *
      24             :  *
      25             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
      26             :  */
      27             : 
      28             : #include <stdexcept>
      29             : 
      30           0 : class UserException : public std::exception
      31             : {
      32             : public:
      33           0 :         virtual const char * what () const throw () override
      34             :         {
      35           0 :                 return "User Exception";
      36             :         }
      37             : };
      38             : 
      39             : namespace kdb
      40             : {
      41             : 
      42           0 : class Exception : public UserException
      43             : {
      44             : public:
      45             :         virtual const char * what () const throw () override
      46             :         {
      47             :                 return "User Exception: Exception thrown by Elektra";
      48             :         }
      49             : };
      50             : 
      51             : class KeyException : public Exception
      52             : {
      53             : public:
      54             :         virtual const char * what () const throw () override
      55             :         {
      56             :                 return "User Exception: Exception thrown by a Key";
      57             :         }
      58             : };
      59             : 
      60             : class KeyNotFoundException : public Exception
      61             : {
      62             : public:
      63             :         explicit KeyNotFoundException (std::string const &)
      64             :         {
      65             :         }
      66             :         virtual const char * what () const throw ()
      67             :         {
      68             :                 return "User Exception: Key not found";
      69             :         }
      70             : 
      71             : private:
      72             : };
      73             : 
      74             : class KeyTypeMismatch : public KeyException
      75             : {
      76             : public:
      77             :         virtual const char * what () const throw () override
      78             :         {
      79             :                 return "User Exception: Binary or String key mismatch";
      80             :         }
      81             : };
      82             : 
      83             : class KeyInvalidName : public KeyException
      84             : {
      85             : public:
      86             :         virtual const char * what () const throw () override
      87             :         {
      88             :                 return "User Exception: Invalid Keyname";
      89             :         }
      90             : };
      91             : 
      92             : class KeyTypeConversion : public KeyException
      93             : {
      94             : public:
      95             :         virtual const char * what () const throw () override
      96             :         {
      97             :                 return "User Exception: Exception thrown by get/set";
      98             :         }
      99             : };
     100             : } // namespace kdb
     101             : 
     102             : #define USER_DEFINED_EXCEPTIONS
     103             : #include <key.hpp>
     104             : 
     105             : namespace kdb
     106             : {
     107             : 
     108             : class KDBException : public Exception
     109             : {
     110             : public:
     111           0 :         explicit KDBException (Key key) : m_key (key)
     112             :         {
     113           0 :         }
     114             : 
     115           0 :         virtual ~KDBException () throw ()
     116           0 :         {
     117           0 :         }
     118             : 
     119           0 :         virtual const char * what () const throw () override
     120             :         {
     121           0 :                 return "User Exception: KDB";
     122             :         }
     123             : 
     124             : protected:
     125             :         Key m_key;
     126             : };
     127             : 
     128             : class ContractException : public KDBException
     129             : {
     130             : public:
     131             :         explicit ContractException (Key key) : KDBException (key)
     132             :         {
     133             :         }
     134             : 
     135             :         ~ContractException () noexcept override = default;
     136             : 
     137             :         const char * what () const noexcept override
     138             :         {
     139             :                 if (!m_key)
     140             :                 {
     141             :                         return "Malformed contract";
     142             :                 }
     143             :                 return KDBException::what ();
     144             :         }
     145             : };
     146             : 
     147             : } // namespace kdb
     148             : 
     149             : 
     150             : #include <kdb.hpp>
     151             : 
     152           0 : int main ()
     153             : {
     154           0 :         kdb::Key k ("abc", KEY_END);
     155           0 :         kdb::KDB kdb;
     156           0 :         kdb::KeySet ks;
     157           0 :         kdb.get (ks, k);
     158           0 : }

Generated by: LCOV version 1.13