eigene Exception Klasse



  • Hallo zusammen,

    ich habe eine eigene Exception Klasse:

    #ifndef A_EXCEPTION_H
    #define	A_EXCEPTION_H
    
    #include <string>
    #include <exception>
    
    namespace A {
        namespace B {
    
            class Exception : public std::exception {
    
            public:
    
                Exception(const char* message) {
                    m = message;
                }
    
                virtual ~Exception() throw() {
                };
    
                const char* toString() {
                    return m.c_str();
                }
    
                const char* what() {
                    return m.c_str();
                }
    
            protected:
                std::string m;
    
            private:
                Exception();
            };
    
            class NotImplementedYetException : public Exception {
            public:
                NotImplementedYetException(const char* message):Exception(message){}
            };
    
            class IndexOutOfBounceException : public Exception {
            public:
                IndexOutOfBounceException(const char* message):Exception(message){}
            };
    
            class AttributeNotSetException : public Exception {
            public:
                AttributeNotSetException(const char* message):Exception(message){}
            };
    
            class PeerNotFoundException : public Exception {
            public:
                PeerNotFoundException(const char* message):Exception(message){}
            };
    
            class ClusterNotFoundException : public Exception {
            public:
                ClusterNotFoundException(const char* message):Exception(message){}
            };
    
            class PeerPlaceException : public Exception {
            public:
                PeerPlaceException(const char* message):Exception(message){}
            };
    
            class ClusterLimitReachedException : public PeerPlaceException {
            public:
                ClusterLimitReachedException(const char* message):PeerPlaceException(message){}
            };
        };
    };
    #endif
    

    Wenn die Exception aber geworfen wird, bekomme ich in meiner Testumgebung folgende Ausgabe bei What():

    N6Ae10B21PeerNotFoundExceptionE
    

    Wie bekomme ich dies gefixt?

    Gruß, Uwe



  • Dann zeige mal wie du die Exception wirfst und fängst...



  • Hallo Theta,

    also werfen zum Beispiel so:

    Peer & Group::getPeer(PeerID pId){ 
    
                for(size_t i = 0; i < clusterList.size(); i++){
    
                    Cluster c = clusterList.get(i);
    
                    if(c.contains(pId)){
                        return c.getPeer(pId);
                    }//End if
    
                }//End for
    
                throw PeerNotFoundException("given peer not in list");
    
            }//End getPeer
    

    Fangen überlasse ich CppUnit 😉
    Gruß, Uwe



  • aha.. und hast du nicht weiter gelesen? fangen auch noch.



  • Die Signatur deiner what-Funktion ist falsch:

    virtual const char* what() const throw()

    Lars



  • Hallo Theta,

    na wenn ich es selber fange, dann so:

    try{
            Peer p = c.getPeer(2);
        }
        catch(A::B::PeerNotFoundException e){
            std::cout<<"Upps: "<<e.what()<<std::endl;
        }
    

    So kommt dann ne schöne Ausgabe ala "Upps: peer not in list". Aber wenn es halt nicht gefangen wird von mir, oder einem Framework wie CppUnitTest, sieht es aus, wie beschrieben.
    Oder kann man da nix ändern? Kann es sein, dass meine what falsch deklariert ist? Muss ich die vielleicht ändern?

    Gruß und Dank



  • Exceptions besser per Referenz fangen.



  • Ah!

    Der manni hatte Recht. Die falsche Signatur war der Grund! Sehr schön, sehr schön 🙂

    Vielen Dank für Eure Hilfe! Fall Gelöst!


Anmelden zum Antworten