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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:36:39+00:00 2026-05-24T11:36:39+00:00

internal static string ReadCSVFile(string filePath) { try { … … } catch(FileNotFoundException ex) {

  • 0
internal static string ReadCSVFile(string filePath)
{
    try
    {
        ...
        ...
    }
    catch(FileNotFoundException ex)
    {
        throw ex;
    }
    catch(Exception ex)
    {
        throw ex;
    }
    finally
    {
        ...
    }
}


//Reading File Contents

public void ReadFile()
{
    try
    {
        ...
        ReadCSVFile(filePath);
        ...
    }
    catch(FileNotFoundException ex)
    {
        ...
    }
    catch(Exception ex)
    {
        ...
    }
}

Here in the above code sample, I have two functions ReadFile and ReadCSVFile.
In the ReadCSVFile, I get an exception of type FileNotFoundException, which gets caught in the catch(FileNotFoundException) block. But when I throw this exception to be caught in the catch(FileNotFoundException) of the ReadFile Function, it gets caught in the catch(Exception) block rather than catch(FileNotFoundException). Moreover, while debugging, the value of the ex says as Object Not Initialized. How can I throw the exception from the called function to the caller function’s catch block without losing the inner exception or atleast the exception message?

  • 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-24T11:36:40+00:00Added an answer on May 24, 2026 at 11:36 am

    You have to use throw; instead of throw ex;:

    internal static string ReadCSVFile(string filePath)
    {
        try
        {
            ...
            ...
        }
        catch(FileNotFoundException ex)
        {
            throw;
        }
        catch(Exception ex)
        {
            throw;
        }
        finally
        {
            ...
        }
    }
    

    Besides that, if you do nothing in your catch block but rethrowing, you don’t need the catch block at all:

    internal static string ReadCSVFile(string filePath)
    {
        try
        {
            ...
            ...
        }
        finally
        {
            ...
        }
    }
    

    Implement the catch block only:

    1. when you want to handle the exception.
    2. when you want to add additional information to the exception by throwing a new exception with the caught one as inner exception:

      catch(Exception exc) { throw new MessageException("Message", exc); }

    You do not have to implement a catch block in every method where an exception can bubble through.

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

Sidebar

Related Questions

Here is the code: internal static void UpdateErrorLogTable(string type, string location, string method, Exception
class Program { internal delegate int CallBack(int i); static void Main(string[] args) { CallBack
I have follow code to make a String to a CFString: internal static byte[]
why the following code doesn't work? internal static string[] GetToolsForRole(string selectedRole) { string[] tempStr;
Given the method: internal static DataSet SelectDataSet(String commandText, DataBaseEnum dataBase) { var dataset =
Let's assume i got this code: internal static bool WriteTransaction(string command) { using (SqlConnection
So I just tried creating a new classification type, [Export(typeof(ClassificationTypeDefinition))] [Name(String)] internal static ClassificationTypeDefinition
I have a method to Generate Expression By Clause as below: internal static Expression<Func<TModel,
This MSDN article stated that: processors are free to reorder this code internal static
I am using following query to insert some large text data : internal static

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.