dolfinx icon indicating copy to clipboard operation
dolfinx copied to clipboard

Using exception subtypes

Open jhale opened this issue 3 years ago • 2 comments

SonarCloud complains about our exception throwing, and I think it makes sense:

If you throw a general exception type, such as std::exception, std::logic_error or std::runtime_error, it forces consumers to catch all exceptions, including unknown exceptions they don’t necessarily know how to handle.

Instead, either throw a subtype that already exists ( for example in ), or create your own type that derives from a standard one.

https://wiki.sei.cmu.edu/confluence/display/java/ERR07-J.+Do+not+throw+RuntimeException%2C+Exception%2C+or+Throwable

jhale avatar Oct 07 '22 09:10 jhale

I took a look at the exceptions. As I see it, it is best to throw your own exception in many places.

Should I create a separate file for my own exception classes? Or place the exception classes in the files in which they are used?

nils-imhoff avatar Jan 14 '23 08:01 nils-imhoff

Hi @nils-imhoff , If the exception classes are only used within a single file, I think they might be placed at the beginning of the file. Alternatively, you might create a new file containing the exceptions, if they are used multiple times.

IgorBaratta avatar Jan 14 '23 12:01 IgorBaratta