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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:00:24+00:00 2026-06-01T09:00:24+00:00

I have the below code: public void OpenFile(string FileName) { if (FileName == null)

  • 0

I have the below code:

        public void OpenFile(string FileName)
        {
            if (FileName == null)
                throw new ArgumentNullException("FileName", "OpenFile: Filename is null");


            List<int> readItems = new List<int>();
            using (StreamReader reader = new StreamReader(FileName))
            {
                string line;
                int batchItem;
                while ((line = reader.ReadLine()) != null)
                {
                    if (int.TryParse(line, out batchItem))
                    {
                        readItems.Add(batchItem);
                    }
                }
            }

            CurrrentFile = FileName;
            FileInfo f = new FileInfo(FileName);
            lock (LockObject)
            {
                TextWriter = f.AppendText();
                TextWriter.AutoFlush = true;
            }

            if (readItems.Count > 0)
                FileOpened(readItems);


        }

I am trying to detect possible problems eg/ Filename is null.

In the class that catches and logs exceptions I obviously have a catch(ArgumentNullException ex)

Should I also be catching possible exceptions thrown by the StreamReader constructor and FileInfo constructor?

I know that sounds silly but I wondered whether I should have explicit catches for the exceptions I’m throwing and then a general exception catch whether it be a catch(Exception ex) or have a try catch around the above code and rethrow a custom exception. Otherwise my try/catch block has about 12 separate catch statements!

  • 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-06-01T09:00:25+00:00Added an answer on June 1, 2026 at 9:00 am

    In general if you follow a few simple rules you can drastically simplify your Exception Handling code.

    1. Only catch and handle exceptions you can actually do something about

    If you can’t actually recover from an exception, then you shouldn’t catch it at all. Let the exception bubble up to the highest possible point (usually the User Interface).

    When you do catch exceptions you should catch very specific exceptions, and avoid generic exception handling if at all possible.

    try
    {
       DoSomeStuff();
    }
    catch(HolyCrapItBlewedUpException ex)
    {
       RecoverFromExplosion();
    }
    catch(Exception ex)
    {
       //I really have no idea what happened, and I can't do
       // anything about it, but I'm going to catch the
       // exception anyway cause it makes me feel better
    }
    

    There are cough exceptions to the rule above, but that brings us to our second rule.

    2. Only use generic exception handling for logging, and implementation hiding

    Since you should be letting your exceptions bubble all the way up to the highest layer, anything that makes it to the top is a bug. At that point, you should be logging that exception and presenting some friendly message to the user.

    try
    {
       DoSomeStuff();
    }
    catch(Exception ex)
    {
       //At this point it's a bug... we need to squash it!
       LogException(ex);
       ShowUserRecoveryOptions();
    }
    

    Another place you might want to do this is in order to maintain a contract for an API, and hide implementation details. If the end user is calling a method, they may not need to know every possible problem that occurred, only that it didn’t work.

    public void MakeMeASandwich()
    {
       try
       {
          MakeCallerASandwich();
       }
       catch(SecurityException ex)
       {
          //It's still best to distinguish between certain exceptions
          // as long as it makes sense to the caller.
          throw new NoIWillNotMakeYouASandwichException(ex);
       }
       catch(Exception ex)
       {
          throw new SorryICantMakeYouASandwichException(ex);
       }
    }
    

    3. Avoid Exceptions Like The Plague!

    Exceptions should be, well… exceptional! In most instances you should be able to avoid exceptions by simply coding for them in advance. A real exception should represent something you couldn’t predict would happen. A simple example is checking for the existence of a file.

    try
    {
       File.Open("blah.txt");
    }
    catch(FileNotFoundException ex)
    {
       File.Create("blah.txt");
    }
    

    In that example, I knew it was possible the file would not exist… so I should have written code to make sure that case almost never happens.

    var fileName = "blah.txt";
    
    if(!File.Exists(fileName))
       File.Create(fileName);
    
    File.Open(fileName);
    

    Now, it is still possible for a FileNotFoundException to be thrown here, but how would we handle it? Clearly this code can no longer do anything about it since something truly EXCEPTIONAL happened. We can let the exception bubble up to the next layer since there is nothing meaningful we can do here.

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

Sidebar

Related Questions

I have wrote below code. import java.io.*; class Test { public static void main(String
I have the code below : public class Anything { public int Data {
I have below code snippet SimpleDateFormat dateFormat = new SimpleDateFormat( yyyy-MM-dd hh:mm:ss.SSS); String processedContentDate=2012-04-10
I have some code like this: public void SaveImage(int Counter) { var task =
I have putting such data into LinearView. as like below code: public void showResult()
I have the following code: public void DeleteAccountsForMonth(int year, int month) { var result
I have below code which overrides equals() and hashcode() methods. public boolean equals(Object obj)
I have a simple code below: import java.util.ArrayList; public class BoidList extends ArrayList {
I have below code behind in c# if (Session[cmpDictionaryTitle]!= null) { downloadLinks.Text += @<li><a
I have this below code access the ListView item value into string and display

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.