Sort of Exception C#

Shervan360 1,481 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

C#
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.
10,216 questions
0 comments No comments
{count} votes

Accepted answer
  1. P a u l 10,406 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