Bird
Raised Fist0

In a method reading a file, you want to handle FileNotFoundException, IOException, and any other exceptions separately. What is the best order of catch blocks?

hard🚀 Application Q8 of Q15
C Sharp (C#) - Exception Handling

In a method reading a file, you want to handle FileNotFoundException, IOException, and any other exceptions separately. What is the best order of catch blocks?

Acatch (FileNotFoundException) { } catch (IOException) { } catch (Exception) { }
Bcatch (Exception) { } catch (IOException) { } catch (FileNotFoundException) { }
Ccatch (IOException) { } catch (FileNotFoundException) { } catch (Exception) { }
Dcatch (Exception) { } catch (FileNotFoundException) { } catch (IOException) { }
Step-by-Step Solution
Solution:
  1. Step 1: Understand exception hierarchy

    FileNotFoundException inherits from IOException, which inherits from Exception.
  2. Step 2: Order catch blocks

    Catch the most specific (FileNotFoundException) first, then IOException, then general Exception.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Specific to general exception order [OK]
Quick Trick: Catch exceptions from most specific to most general [OK]
Common Mistakes:
MISTAKES
  • Catching general exceptions before specific
  • Mixing order of related exceptions
  • Ignoring inheritance hierarchy

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes