Why include stdexcept and new instead of exception?

I discovered something interesting that I'm quite puzzled by which has resulted in a question I have that I can't google about. I took two example programs my book has. One uses #include <stdexcept> and the other uses #include <new> I tested an self-edited version of both programs where I replaced stdexcept and new with exception #include <exception> and they both worked. So my question is why bother writing #include <stdexcept> or ]code]#include <new> [/code] when #include <exception> can do both.

I myself believe that its partially to cut down on the amount of files you would have to include and if you're releasing an executable you wouldn't want a bunch of extra unused libraries inside of it. Just like <iostream> includes a bunch of the code needed for I/O functions, you probably will not use all of it , so could just include a more basic library such as <fstream>.
Oh yes I see what you mean it reduces overhead. heh I didn't think of that. Thanks.
closed account (Dy7SLyTq)
@dryand: if you include <string> and dont use it, its not going to show up in the binary. its just going to slow down compile time (of course if you really wanted the library to show up you could probably get around this by compiling it into a dll but thats offtopic). also you have that backwards. istream, ostream, ifstream, ofstream all derive from basic_stream. neither is more basic than the other, just controlling where the io is being mapped to

Why bother writing <stdexcept>? I guess you'd want to if you were using stuff that was defined/declared only there.

Code that relies on some standard header including some other standard header is non-portable. That code may break in future versions of the library or when using different compiler or tool chain configurations.

Don't think you've figured out a shortcut. Include the headers that are required for the code you are writing so your code doesn't break unexpectedly.
Topic archived. No new replies allowed.