Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6889085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:06:41+00:00 2026-05-27T06:06:41+00:00

In the following chunk of code the new constructor is documented to throw seven

  • 0

In the following chunk of code the new constructor is documented to throw seven different exception types, including System.IO.PathTooLongException and System.ArgumentException, System.UnauthorizedAccessException and System.SecurityException:

try
{
    var fileInfo = new FileInfo(path);
}
catch ???

I’m just trying to ensure that the path is actually an accessible path and nothing bad happens when I try to create a file using this path.

So, the question:

Books and coding guidelines tell me that I shouldn’t use catch (Exception) construct, but I face the following situation – I can catch and handle 4 (out of 7) exception types and their handling for every exception types is absolutely the same.

How should this be done?


I can also think of the following solution, but it still looks bad:

catch (Exception exception)
{
    Debug.Assert(exception is PathTooLongException || exception is ...);

    // (or maybe rethrow it further if there is a type mismatch)

    //... Handling code ...
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:06:41+00:00Added an answer on May 27, 2026 at 6:06 am

    You don’t have to catch every possible exception around every line of code.

    Instead, only catch when you have context and you are prepared to handle and take appropriate action. Otherwise, just let it bubble up.

    Catching exceptions (especially deep in the stack in common functions where you don’t have enough context to handle) can be problematic. It sometimes leads to swallowing and not appropriately handling. Handle exceptions when you have context.

    For that FileInfo exception it depends how deep in that stack it is. If it’s in a library where it can be reached via multiple paths and scenarios, you shouldn’t handle, what are you going to do at that point? If that’s also the case and it bubble all the way out through a deep stack to a GUI performing an operation where you want to handle it, what are you going to catch? Every possible exception of every code path on it’s execution path? And, as code changes, you’re going to re-evaluate all paths? In a shallow code path (file info dialog that calls that method) it’s an easy call but even then you’re not going to handle it differently – you’re going to display an error dialog or message in panel. Exception types offer the ability to handle differently. In both of those case, you handle exception so I say bah hum bug to that guideline.

    What is very clear is you should not throw type Exception. That short circuits the ability to handle differently.

    Another related post:

    Trying to understand exceptions in C#

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say you have the following chunk of code: <div id=container> <someelement>This is any
I came across the following weird chunk of code.Imagine you have the following typedef:
At the following URL: https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsICacheVisitor is the following code chunk: boolean visitDevice(in string deviceID,
I have following code to serialize my data into a file: out = new
The following is the chunk of my code that is attaching a movieclip (from
I have the following code: public class Search { private Desktop desktop = new
I have the following chunk of code in my footer.phtml <!-- Customer Modal -->
I have the following chunk of Python code (running v2.7) that results in MemoryError
On an ASP.net site at my place of work, the following chunk of code
I am having the following code snippet to upload zip file without conversion to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.