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 customize C++ type exception

2025-02-25 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 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!

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