question

Shervan360-6172 avatar image
0 Votes"
Shervan360-6172 asked Paul-5034 answered

Sort of Exception C#

Hello,

Please see the following photo from C# Book.

Isn't it better to write FormatException first and then SystemException? because Format Exception is a more specific class than System Exception.
If Format Exception occurs, the first catch section (System Exception) will handle it because a Format Exception is a type of System Exception. and never Format exception will execute when Format Exception occurs.


143142-screenshot-2021-10-23-094431.jpg


dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Paul-5034 avatar image
0 Votes"
Paul-5034 answered

Not only is it better - it's mandatory. The compiler will give you an error if you're catching a less-derived Exception before a more-derived Exception:

143152-image.png



image.png (16.2 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.