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

What if C++ can't throw an exception?

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

Share

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

This article mainly explains "what to do if C++ cannot throw an exception". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what if C++ can't throw an exception?"

E.26: if an exception cannot be thrown, handle the failure as soon as possible

Reason (reason)

If you can't do a good job at recovering, at least you can get out before too much consequential damage is done.

If you can't recover well from your mistakes, at least you can quit before more harm occurs.

Note (Note)

If you cannot systematically handle errors, think of "failure" as a response to any errors that cannot be dealt with locally. That is, if you can't recover from the error in the context of the function that detected the problem, you can call the about function, the quick_exit () function, or similar function that can trigger a system restart.

In a system that contains a lot of tasks or a large number of computers, you already need to anticipate and deal with fatal failures (including hardware errors). In such cases, "failure" simply transfers error handling to the next layer of the system.

Example (sample)

Void f (int n)

{

/ /...

P = static_cast (malloc (n * sizeof (X)

If (! P) abort (); / / abort if memory is exhausted

/ /...

}

Most programs cannot handle memory exhaustion gracefully anyway. This is roughly equivalent to

Most programs can't handle memory exhaustion satisfactorily. This is almost equivalent to the following code:

Void f (int n)

{

/ /...

P = new X [n]; / / throw if memory is exhausted (by default, terminate)

/ /...

}

Typically, it is a good idea to log the reason for the "crash" before exiting.

Usually, it's a good idea to record the reason before quitting because of a "failure".

At this point, I believe you have a deeper understanding of "what if C++ cannot throw an exception". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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