How to throw good exceptions?

前端 未结 12 1359
情话喂你
情话喂你 2020-12-14 21:05

I heard you should never throw a string because there is a lack of information and you\'ll catch exceptions you dont expect to catch. What are good practice for throwing exc

12条回答
  •  执念已碎
    2020-12-14 21:55

    I always throw an exception with a message of where it occurred and what caused it to happen:

    throw NException("Foo::Bar", "Mungulator cause a stack overflow!");
    

    You can then use these strings in messageboxes etc.

    I always catch via

    catch (NException& ex) { ... }
    

    If you running windows you can pass the error value and have a function derive the error message. The best example of this is in Windows via C/C++ by Jeffrey Richter.

提交回复
热议问题