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 5965181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:33:59+00:00 2026-05-22T19:33:59+00:00

At first, I was going to do something like the following: public void WriteToFile(string

  • 0

At first, I was going to do something like the following:

public void WriteToFile(string filePath, string contents)
{
    try
    {
        File.WriteAllText(filePath, contents)
    }
    catch(Exception ex)
    {
        //Log error
    }
}

But then I decided to catch all the specific exceptions for the method WriteAllText, like the following:

public void WriteToFile(string filePath, string contents)
{
    try
    {
        File.WriteAllText(filePath, contents);
    }
    catch (IOException ex)
    {
        //An I/O error occured when opening the file
    }
    catch (ArgumentException ex)
    {
       //The exception that is thrown when one of the arguments provided to a method   
         that is not valid.
    }
    catch (UnauthorizedAccessException ex)
    {
       //Unauthorized access
    }
    catch (SecurityException ex)
    {
        //Security exception
    }
    catch (NotSupportedException ex)
    {
        //Invoked method not supported
    } 
}

The above is very verbose and with other methods, it could be more. Is there a better way to do this so I don’t have to write so many catch statements. Also, if an exception is caught, is it best to return from it, log it. I always get confused on how to handle it.

I have noticed some confusion. I am going to handle the exceptions, I left out handling the exception to keep this short. I am going to make use of the ex variable. The question is more about doing just catch(Exception ex) or multiple catch statements.

I also bring this up because I always here that it is better to handle specific exceptions rather than a catch-all. If I have misunderstood this, please clarify on what it means.

  • 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-22T19:34:00+00:00Added an answer on May 22, 2026 at 7:34 pm

    It depends on how you are handling the exception. For example, if a SecurityException will cause you to present a dialog to the user to provide their credentials, then you should have a separate catch clause. If not, there is no need to explicitly call them all out.

    E.g.

    try
    {
        File.WriteAllText(filePath, contents);
    }
    catch (SecurityException ex)
    {
        //present dialog
    }
    catch (Exception ex)
    {
        //All other exceptions handled the same
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Whenever I do something like the following: public class MyDto { [Key] public int
I've got something like the following XML file. <credits> <property name=tag> <item>v0003</item> </property> <property
Imagine the following simple code: public void F<T>(IList<T> values) where T : struct {
First I'm going to show you an image of what I'm trying to recreate
First I've read loads of posts and sites that recommend going to http://silverlight.net/GetStarted/ to
I am going to develop my first application (4 members team).I am not aware
First of all, I have to say that I'm going to talk about System.ComponentModel.Component
First, a little background, because there is a lot of interaction going on: I'm
I'm going a little mad I think. This is the VERY first thing I
Yesterday I used jQuery UI for the first time and I think I'm going

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.