I am working on a small c++ program and learning exceptions. Is the following code \"bad\", and if so, what can I do to improve it?
try {
// code
if
If your idea of throwing a string as an error was for convenience of showing the error to the user, consider that this would make localizing your application more difficult (may or may not be a concern to you though).
Also if another part of the application needs to understand what the error is so it can react to it (eg if it is a disconnect error, try to reconnect automatically, but if it's a password error, just show error message to the user), it is better to have some sort of error code available to the exception catcher.
In our app, our exceptions are derived from std::exception. They contain an error type (an enum), a debug error message (including file/line number), and a localized error string.