c++ - Why Create An Exception Object That's Derived From stdexcept Header? -


I was wondering why, and if so, what's the benefit, you will create an exception object to throw an exception, which I can actually find out why Stdexcept headers, such as an exception object that is received from runtime_error, can actually find out why you do this, you can use the 'functional' key to get a suitable error message. But you can not simply pass the error message in runtime_error as an argument, and you can use 'what' anyway?

  class divide_by_zero: public runtime_error {public: divide_by_zero (): runtime_error ("this is a runtime error") {}; } If (...) {Feng divide_by_zero (); }   

How is it different from doing something like this ...

  if (...) {std :: runtime_error ("This is a runtime Error is "); } {...} Catch (std :: runtime_error & amp; e) {std :: cerr & lt; & Lt; E.what () & lt; & Lt; Std :: endl; }  "post-text" itemprop = "text"> 

Retired ninja commented that "you can catch the derivative exception without catching all Runtime_error exception." Is an important point. Your code can expect your divide_by_zero and wants to ignore it or log in, but runtime_error to std :: locale :: locale < / Code>

Next, note that if you use the same class and what () - rely on the comparison of the first text, then it is very "delicate "As the throwing site usually works to improve its clarity and usefulness in conveying messages again. Expects to be. It is also slow.

If you use your own class, you can more easily enrich it later in a structured way, perhaps Adding some details, due to error or error, the process that can be used easily and reliably in other variables. After embedding new data of that lesson, it can be reversed with the real trick, which can mess with the existing code with expectations about the message "style" and potential length (for example it is a dialog box " Blowing out ", so it will not fit properly on the screen), and the text is either rigidly structured (eg moved to an example XML) or parsing requires error prone ....

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -