Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

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

Shulou Source: shulou.com Published: 2022-06-01 20:48:08 10月29日 Update

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!

Tags: Error processing function system what to do Clearing content reason more learning practical deeper almost up and down context that is code task interest Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Apple Docker MySQL Huawei