When exception types are included in a method specification, only those exception types may be thrown by the method. If an attempt is made to throw anything else, then by default a ``++std::bad_exception++`` is thrown. If ``++std::bad_exception++`` is not itself listed in the method specification, then the end result is that ``++terminate()++`` is called, resulting in an implementation-defined termination of the program.


Methods that don't include exception types in their specifications can throw any exception type. However, this fact should not be taken as an argument for omitting exception types. It is far better to thoroughly specify a method, so that callers know what to expect, than to leave them in the dark.


Therefore, all exceptions that could be thrown by a method should be explicitly listed in its specification.


== Noncompliant Code Example

----
void foo () // no exceptions specified
{
  throw ( 21 ); // anything can be thrown
}

void goo ( ) throw ( Exception ) 
{ 
  foo ( ); // Noncompliant; an int could be thrown
}
----


== Compliant Solution

----
void foo () // no exceptions specified
{
  throw ( 21 ); // this is legal; anything can be thrown
}

void goo ( ) throw ( Exception, int ) 
{ 
  foo ( );
}
----


== See

* MISRA {cpp}:2008, 15-5-2
* http://cwe.mitre.org/data/definitions/391.html[MITRE, CWE-391] - Unchecked Error Condition
* https://www.securecoding.cert.org/confluence/x/EADTAQ[CERT, ERR55-CPP.] - Honor exception specifications


ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)

include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]
