Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including an exception class in a header file

Tags:

c++

exception

I have a pair of files, say Foo.cpp and Foo.hpp that define a class, say Foo. Foo's methods have the potential to throw an exception, defined in the file MyException.hpp, but Foo.hpp does not need MyException.

Obviously the .cpp needs to include the exception header, but should I include the exception header in Foo.hpp instead to allow any files that use Foo to have a definition of the exception?

like image 510
Xenopathic Avatar asked Nov 19 '25 14:11

Xenopathic


1 Answers

The usual rule of thumb is that every file should include the headers that it needs. So, no, don't add MyException.hpp to Foo.hpp unless it's needed there. Code that deals with your exception class should #include "MyException.hpp". So, obviously, whichever files implement the member functions that throw this exception need to include "MyException.hpp", and any files that include code that catch this exception also need it.

like image 75
Pete Becker Avatar answered Nov 22 '25 03:11

Pete Becker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!