Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including <exception> header C++ [duplicate]

Tags:

c++

exception

It seems when we include <iostream> header, <exception> and <stdexcept> headers are included automatically.

Question is why the reference sites like cppreference and cplusplus.com include <exception> while explaining exception handling?

Is it necessary to include <exception> or <stdexcept>?

like image 560
Alireza Avatar asked Oct 20 '25 04:10

Alireza


2 Answers

You should always follow documentation. When documentation says that in order to use ceratain construct you need to include certain header, the header must be included. Otherwise, tomorrow iostream will stop including the header, and your program will fail to compile or worse - will behave unexpectedly.

like image 57
SergeyA Avatar answered Oct 22 '25 18:10

SergeyA


You should always include what you use. The C++ standard doesn't state that any particular header has to include another, they are free to do so for convenience. But note that just because that happens to be the case for one compiler, it may not be the case on another (e.g. Visual Studio vs gcc)

like image 25
Cory Kramer Avatar answered Oct 22 '25 17:10

Cory Kramer