1

In my project, I'm setting up an error handling system, but before I put in the code, I setup a test project to get a foundation to build off of. I have everything I need, except for one thing, which is the error.ToMessage() function. Problem is, I don't know how to set up the function in the way I have built it and can't find examples.

Here is my code:

#include <iostrea> enum class ErrorCode { StreamError, FileError, CloseError }; int main() { try { throw ErrorCode::StreamError } catch (ErrorCode err) { //std::cout << err.ToMessage() << std::endl; //This is how I would like to output the error. switch (err) { case ErrorCode::StreamError: std::cout << "Stream Error"; //This is how it's currently done. //... } } } 

How can I do this, if it's possible?

2
  • Create class ErrorCode (should inherit from std::exception), not enum. Commented Dec 18, 2014 at 18:08
  • See this reference and you can see under standard exceptions an example of what Ed S has stated in his answer. Commented Dec 18, 2014 at 18:10

6 Answers 6

2

You'll need more than an enum to accomplish this. What you can do is to create your own exception type which inherits from std::exception. This class would have an ErrorCode member (for your enum) as well as a what() method, which returns a string (already there for you in std::exception).

Sign up to request clarification or add additional context in comments.

Comments

1

This is an addition to all the other answers, which recommend creating your own exception class (and inheriting std::exception, which is a commonly recommended practice).

If you don't want to go that way, consider making a free-standing (global) function:

enum class ErrorCode { StreamError, FileError, CloseError }; std::string ToMessage(ErrorCode e) { switch (err) { case ErrorCode::StreamError: return "Stream Error"; case ... default: ... } } 

You can use any implementation inside the function: a switch statement, a LUT or a std::map.

1 Comment

This is not a good idea because code that's based on standard exceptions won't know anything about ToMessage.
1

You have to make a class that handle the exception and put all the switch case in this class that will generate the string for all exception catched

Comments

1

The standard library's solution is the std::system_error class, which is derived from std::runtime_error, which is derived from the top level exception class std::exception.

std::exception::what can be used to obtain an exception message.

If you find the standard class to be unsuitable for your purposes, then derive a class from std::runtime_error or from std::system_error.

Comments

0

Duplicate as: C++ catch enum value as exception

You can put enum in a derived classs from std::exception and then handle the exception in there

Comments

-1
#include <iostream> #define enum_name(a) #a enum test_enum { LOL, OKAY, NOPE, YOU, ARE, SEXY }; int main() { auto test = test_enum::SEXY; std::cout << enum_name(test); return 0; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.