Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to customize C++ type exception

Shulou Source: shulou.com Published: 2022-06-01 20:33:43 09月29日 Update

This article mainly explains "how to customize C++ type 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 "how to customize the type exception of C++"!

E.14: using custom type exceptions designed for purpose (not built-in types)

Reason (reason)

A user-defined type is unlikely to clash with other people's exceptions.

User-defined types are unlikely to conflict with other people's exceptions.

Example (sample)

Void my_code ()

{

/ /...

Throw Moonphase_error {}

/ /...

}

Void your_code ()

{

Try {

/ /...

My_code ()

/ /...

}

Catch (const Bufferpool_exhausted&) {

/ /...

}

} Example, don't (negative example) void my_code () / / Don't

{

/ /...

Throw 7; / / 7 means "moon in the 4th quarter"

/ /...

}

Void your_code () / / Don't

{

Try {

/ /...

My_code ()

/ /...

}

Catch (int I) {/ / I = = 7 means "input buffer too small"

/ /...

}

} Note (note)

The standard-library classes derived from exception should be used only as base classes or for exceptions that require only "generic" handling. Like built-in types, their use could clash with other people's use of them.

Standard library classes that inherit from exception should be used only for base classes or exceptions that require only "normal" handling. Similar to built-in types, your use of them may conflict with the use of others.

Example, don't (negative example)

Void my_code () / / Don't

{

/ /...

Throw runtime_error {"moon in the 4th quarter"}

/ /...

}

Void your_code () / / Don't

{

Try {

/ /...

My_code ()

/ /...

}

Catch (const runtime_error&) {/ / runtime_error means "input buffer too small"

/ /...

}

}

Enforcement (implementation recommendations)

Catch throw and catch of a built-in type. Maybe warn about throw and catch using a standard-library exception type. Obviously, exceptions derived from the std::exception hierarchy are fine.

Capture throw and catch for built-in types. It may be possible to warn against throw and catch that use standard library exception types. Obviously, there is nothing wrong with exception classes that inherit from std::exception.

At this point, I believe you have a deeper understanding of "how C++ customized type exception", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Docker MariaDB Linux NVidia