It seems like you have badly designed exception classes. Why don't you simply add a member-function getMessage to your exception-hierarchy (preferably in the base-class all your exceptions derive from std::exception provides the method what that returns an error message). This way, you could simply catch all exceptions in one statement, and handle them all in the same way.
It seems like you have badly designed exception classes. Why don't you simply add a member-function getMessage to your exception-hierarchy (preferably in the base-class all your exceptions derive from - std::exception provides the method what that returns an error message). This way, you could simply catch all exceptions in one statement, and handle them all in the same way.
Class DeviceResponseException : public std::runtime_error { public: DeviceResponseException() : std::runtime_error("Some error message") {} }; class ATM1212UnlockedException : public std::runtime_error { public: ATM1212UnlockedException() : std::runtime_error("Some other error message") {} }; Then in your code, you can do this: try { std::auto_ptr port(new ACMEPort(12)); port->Open(); } catch( std::runtime_error & e) { ReportPortError(e); logger. Log(e.what(), e); } Since your exception-classes all derive from std::runtime_error, this catch-clause catches them all. This is OK, since the handling is the same for all cases.
If you have specific exceptions that require special handling, you add additional catch-clauses. Also, instead of calling delete port in the exception handler, you should use an std::auto_ptr or boost::scoped_ptr or something of the kind. Read about RAII.
I want to group exceptions that are similar to each other, caused by the same component – dantuch May 5 at 12:58 @dantuch: You can group exceptions by deriving from the same class. You exception hierarchy can have more than two levels. – Björn Pollex May 5 at 13:22 One problem.
According to the standard std::Exception does not take any parameters in its constructor.So it is usually better to derive from one of the exceptions that have been derived from std::exception. Personally I always use std::runtime_error. This works exactly like you want std::exception to work above.
– Loki Astari May 5 at 15:10 @Martin: Thanks, fixed. – Björn Pollex May 57 at 6:28.
Yes, I would: have one single exception class and thus: not translate exceptions from one type to another catch the exceptions much higher up in the code and thus: not end up writing more exception handling code than real code and I would not create objects using new, and if I did I'd use smart pointers Basically, I think you have completely misunderstood how exceptions should be used. The best single change you could make to your code is to replace: ACMEPort* port = new ACMEPort(12); port->Open(); with: ACMEPort port(12); port.Open(); and similarly elsewhere - the need for catching exceptions then goes away.
Don't mind my quiestion if it would take some time. – dantuch May 5 at 12:23 1 You should not just have one single exception class. You should however have one singe base-class for all exception classes.
– Björn Pollex May 5 at 12:23 @Space Matter of opinion. I have got on very well for many years with a single exception class. – nbt May 5 at 12:26 @unapersson so code above looks ok for you?
– dantuch May 5 at 12:28 1 @dantuvh No! It looks awful. – nbt May 5 at 12:29.
I'd move the exception handlers into the class and let the class handle errors instead of forcing all callers to handle it. This way you can do clean up of the pointer (if you really have to have a pointer for some reason) by checking the result value of Open(): class LocalPort { public: LocalPort(int portNumber) { innerPort = new ACMEPort(portNumber); } bool Open() { try { innerPort->Open(); return true; } catch (DeviceResponseException& e) { throw PortDeviceFailure(e); } catch (ATM1212UnlockedException& e) { throw PortDeviceFailure(e); } catch (GMXError& e) { throw PortDeviceFailure(e); } catch (bad_alloc& e) { ReportError(e); logger. Log("Wyjątek alokacji pamięci", e); } catch (PortDeviceFailure& e) { ReportError(e); logger.
Log(e.getMessage(), e); } return false; } private: ACMEPort* innerPort; }; LocalPort* port = new LocalPort(12); if (!port->Open()) { delete port; }.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.