0

I'm just writing my errors as words. Should I catch them as char* v, char v or const char* v?

try{
    int choice = 1
        if(choice != 2)
            throw choice;
}
catch(char* v){
    if (v == choice)
cout << "Error: choice != 2."
}
hexicle
  • 2,121
  • 2
  • 24
  • 31

2 Answers2

9

You should catch an int since you are throwing an int

matt-dot-net
  • 4,204
  • 21
  • 24
4

Try this instead:

try
{
    int choice = 1;
    if(choice != 2)
    {
        throw std::runtime_error("choice != 2");
    }
}
catch(std::exception& e)
{
    std::cout << "Error: " << e.what() << "\n";
}

std::exceptions are the preferred objects to be thrown and the error location knows best to provide a description of what is happening.

leemes
  • 44,967
  • 21
  • 135
  • 183
Enigma
  • 1,699
  • 10
  • 14
  • 1
    I think you should remove the condition `v == choice`, since neither `v` nor `choice` are defined at this place and you want to generally show the exception's "what"-message, independent of which error occurred, right? – leemes Jun 07 '13 at 14:47
  • @leemes: agree on that one. I copied it from the original question – Enigma Jun 07 '13 at 14:48
  • I removed the `v == choice` – Enigma Jun 07 '13 at 14:49
  • Now only two semicolons were missing -- added them. ;) – leemes Jun 07 '13 at 14:53