Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use reference form to catch exceptions in inheritance system in C++

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use the citation form in C++ to catch exceptions in the inheritance system". The content in the article is simple and clear, and it is easy to learn and understand. Now please follow the editor's train of thought slowly and deeply. Let's study and learn how to use citation forms in C++ to catch exceptions in the inheritance system.

E.15: using reference forms to catch exceptions in the inheritance system

Reason (reason)

To prevent slicing.

To avoid truncation.

Example (sample)

Void f ()

{

Try {

/ /...

}

Catch (exception e) {/ / don't: may slice

/ /...

}

}

Instead, use a reference:

Use references instead of:

Catch (exception& e) {/ *... * /}

Or-typically better still-a const reference:

Or-- generally better-- use constant references:

Catch (const exception& e) {/ *... * /}

Most handlers do not modify their exception and in general we recommend use of const.

Most handlers do not change the contents of the exception, so we usually recommend using the constant form.

Note (Note)

Use throw; to rethrow the caught exception; not throw e;. Using throw e; throws a new copy of e (the truncated result of the static type std::exception) instead of rethrowing the original exception of type std::runtime_error. (but stick to it: don't try to catch all exceptions in every function and don't forget to use explicit try/catch as little as possible. )

Enforcement (implementation recommendations)

Flag by-value exceptions if their types are part of a hierarchy (could require whole-program analysis to be perfect).

If the exception type is part of the class hierarchy, mark-passing values are used (which requires more perfect parsing of the entire program).

Thank you for reading, the above is the content of "how to use reference form in C++ to catch exceptions in inheritance system". After the study of this article, I believe you have a deeper understanding of the problem of how to use reference form to catch exceptions in inheritance system in C++, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report