Exception Specification in C++

Exception Specifications and noexcept in C++.

IntermediateException Handling ProgramsExample 4 of 5
exception-specification.cpp
1#include <iostream>
2#include <stdexcept>
3using namespace std;
4
5// Function that may throw exceptions
6int divide(int a, int b) {
7 if (b == 0) {
8 throw runtime_error("Division by zero");
9 }
10 return a / b;
11}
12
13// Function that doesn't throw (noexcept)
14int add(int a, int b) noexcept {
15 return a + b;
16}
17
18// Function with exception specification (deprecated in C++11)
19void mayThrow() throw(runtime_error) {
20 throw runtime_error("This function may throw");
21}
22
23int main() {
24 // noexcept function - safe to call
25 cout << "Add (noexcept): " << add(5, 3) << endl;
26
27 // Function that may throw - use try-catch
28 try {
29 cout << "Divide: " << divide(10, 2) << endl;
30 cout << "Divide by zero: " << divide(10, 0) << endl;
31 } catch (const runtime_error& e) {
32 cout << "Caught: " << e.what() << endl;
33 }
34
35 // noexcept operator - check if function is noexcept
36 cout << "\nadd() is noexcept: " << noexcept(add(1, 2)) << endl;
37 cout << "divide() is noexcept: " << noexcept(divide(1, 2)) << endl;
38
39 return 0;
40}

Output

Add (noexcept): 8
Divide: 5
Caught: Division by zero

add() is noexcept: 1
divide() is noexcept: 0

What's going on

The program demonstrates exception specifications:

Using noexcept specifier
Functions that don't throw exceptions
Checking if function is noexcept
Exception specifications (deprecated)

Exception specifications enable exception safety guarantees.

Header Files Used

1.#include <iostream>
Provides cout and cin for input/output operations.
2.#include <stdexcept>
Provides standard exception classes.

Understanding noexcept

noexcept Concept

:

Specifies function won't throw
Compiler optimization hint
Exception safety guarantee
Modern C++11+ approach

Benefits

:

Compiler optimizations
Clear exception contracts
Better performance
Exception safety guarantees

noexcept Functions

Declaration

:

int add(int a, int b) noexcept {

}

1return a + b;

How it works

:

noexcept: function won't throw
Compiler can optimize
If exception thrown, terminate()
Safety guarantee

Functions That May Throw

Without noexcept

:

int divide(int a, int b) {

if (b == 0) {

throw runtime_error("Division by zero");

}

}

1return a / b;

How it works

:

No noexcept: may throw
Must use try-catch
Handle exceptions
No optimization guarantee

noexcept Operator

Checking noexcept

:

noexcept(add(1, 2)) // Returns true

noexcept(divide(1, 2)) // Returns false

How it works

:

Checks if expression is noexcept
Returns true/false
Compile-time check
Useful for templates

Exception Specifications (Deprecated)

Old Syntax

:

void mayThrow() throw(runtime_error) {

throw runtime_error("Error");

}

How it works

:

Deprecated in C++11
Old exception specification
Use noexcept instead
Not recommended

When to Use noexcept

Best For

:

Functions that never throw
Performance-critical code
Move constructors/assignments
Destructors (usually)
Simple utility functions

Example Scenarios

:

Simple arithmetic
Getters/setters
Move operations
Destructors
Utility functions

Important Considerations

Exception Safety

:

noexcept = guarantee no throw
If exception thrown, terminate()
Be certain before using
Test thoroughly

Performance

:

Compiler optimizations
Better code generation
Reduced overhead
Faster execution

Contracts

:

Clear exception contracts
Document behavior
Help with debugging
Better code understanding

return 0;

This ends the program successfully.