C++ Exceptions - Is throwing c-string as an exception bad?

前端 未结 7 2164
花落未央
花落未央 2020-12-09 15:17

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          


        
7条回答
  •  时光说笑
    2020-12-09 15:34

    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.

提交回复
热议问题