Sort of Exception C#

Shervan360 1,681 Reputation points
2021-10-23T13:55:50.093+00:00

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

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

Answer accepted by question author
  1. P a u l 10,761 Reputation points
    2021-10-23T14:10:42.16+00:00

    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

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.