C++ Exception

Exception Classes for Language Support

Exceptions for language support are used by language features. So in a way they are part of the core language rather than the library. These exceptions are thrown when the following operations fail.

• An exception of class bad_alloc is thrown whenever the global operator new fails (except when the nothrow version of new is used). This is probably the most important
exception because it might occur at any time in any nontrivial program.
• An exception of class bad_cast is thrown by the dynamic_cast operator if a type conversion on a reference fails at runtime.
• An exception of class bad_typeid is thrown by the typeid operator for runtime type identification. If the argument to typeid is zero or the null pointer, this exception gets
thrown.
• An exception of class bad_exception is used to handle unexpected exceptions. It does this by using the function unexpected(). unexpected() is called if a function throws

an exception that is not listed in an exception specification (exception specifications). For example:

class El;
class E2;
void f() throw(E1) //throws only exceptions of type E1
{
...
throw El(); //throws exception of type El

...
throw E2();//calls unexpected(), which calls terminate()
}

The throw of an exception of type E2 in f() violates the exception specification. In this case, the global function unexpected() gets called, which usually calls terminate()

to terminate the program. However, if class bad_exception is part of the exception specification, then unexpected() usually rethrows an exception of this type:

class El;
class E2;
void f() throw(E1, std::bad_exception)
//throws exception of type El or
//bad_exception for any other exception type
{
...
throw El(); //throws exception of type El
...
throw E2(); //calls unexpected(), which throws bad_exception
}

Thus, if an exception specification includes the class bad_exception, then any exception not part of the specification may be replaced by bad_exception within the function
unexpected().

Exception Classes for the Standard Library
Exception classes for the C++ standard library are usually derived from class logic_error. Logic errors are errors that, at least in theory, could be avoided by the program; for example, by  performing additional tests of function arguments. Examples of such errors are a violation of logical preconditions or a class invariant. The C++ standard library provides the following classes for logic errors:
• An exception of class invalid_argument is used to report invalid arguments, such as when a bitset (array of bits) is initialized with a char other than '0' or '1'.
• An exception of class length_error is used to report an attempt to do something that exceeds a maximum allowable size, such as appending too many characters to a string.
• An exception of class out_of_range is used to report that an argument value is not in the expected range, such as when a wrong index is used in an array-like collection or string.
• An exception of class domain_error is used to report a domain error. In addition, for the I/O part of the library, a special exception class called ios_base::failure is  provided. It may be thrown when a stream changes its state due to an error or end-of-file.

Exception Classes for Errors Outside the Scope of a Program

Exceptions derived from runtime_error are provided to report events that are beyond the scope of a program and are not easily avoidable. The C++ standard library provides the following classes for runtime errors:
• An exception of class range_error is used to report a range error in internal computations.
• An exception of class overflow_error is used to report an arithmetic overflow.
• An exception of class underflow_error is used to report an arithmetic underflow.

Exceptions Thrown by the Standard Library
The C++ standard library itself can produce exceptions of classes range_error, out_of_range, and invalid_argument. However, because language features as well as
user code are used by the library, their functions might throw any exception indirectly. In particular, bad_alloc exceptions can be thrown whenever storage is allocated.
Any implementation of the standard library might offer additional exception classes (either as siblings or as derived classes). However, the use of these nonstandard classes makes code nonportable because you could not use another implementation of the standard library without breaking your code. So, you should always use only the standard exception classes.

Header Files for Exception Classes
The base class exception and class bad_exception are defined in <exception>. Class bad_alloc is defined in <new>. Classes bad_cast and bad_typeid are defined in
<typeinfo>. Class ios_base::failure is defined in <ios>. All other classes are defined in <stdexcept>.

原文链接: https://www.cnblogs.com/Podevor/archive/2011/07/05/2788148.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C++ Exception

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/28129

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 上午5:41
下一篇 2023年2月8日 上午5:41

相关推荐