15
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?

sporty_1993
  • 133
  • 10
Anubhav Ranjan
  • 1,558
  • 3
  • 18
  • 32
  • 2
    possible duplicate of [.NET - Throwing Exceptions best practices](http://stackoverflow.com/questions/22623/net-throwing-exceptions-best-practices) – Chris Aug 11 '11 at 10:31
  • as you use the same exception local object, are you sure the inner exception is of type `FileNotFoundException`? – NirMH Aug 11 '11 at 10:28
  • Not a duplicate, but a question lacking informatio to show why it is no duplicate :) – PVitt Aug 11 '11 at 10:37

5 Answers5

20

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.

PVitt
  • 11,500
  • 5
  • 51
  • 85
  • 1
    I tried using only throw but the result remained the same. Moreover, I am actually attaching a custom message to the exception and then throwing it. – Anubhav Ranjan Aug 11 '11 at 10:33
  • 1
    Then you have to throw a new exception with the custom message and the caught exception as inner exception: catch(Exception exc) { throw new MessageException("Message", exc); } – PVitt Aug 11 '11 at 10:35
  • The answer sounds legit, but for some reason it doesn't work- this should be simpler than initializing a new exception object at every caller function. – vivekagarwal277 Jun 29 '23 at 20:16
  • @vivekagarwal277 You only need to create a new outer exception when you want to add information to the inner exception. – PVitt Aug 03 '23 at 20:08
  • @PVitt I don't intend to create an outer exception- I want this exception caught in the correct catch block, i.e. the catch block meant for that particular exception- not the parent class 'Exception'. Like I said- the answer looks right- but for some reason- the exception is always caught in the catch(Exception) block- specially when the caller is in another class. – vivekagarwal277 Aug 06 '23 at 15:17
2

Just use throw in the called function. Dont overload catch blocks with multiple exception types. Let the caller take care of that.

Zenwalker
  • 1,883
  • 1
  • 14
  • 27
2

You should replace

throw ex;

by

throw;
sll
  • 61,540
  • 22
  • 104
  • 156
1

In the called function just use throw like this

 try
 {
   //you code
 }
 catch
 {
     throw;
}

Now, if the exception arise here then this will be caught by the caller function .

Syeda
  • 1,215
  • 11
  • 23
0

Your code works fine here, Check here http://ideone.com/jOlYQ

Zenwalker
  • 1,883
  • 1
  • 14
  • 27
  • Is there any difference if the code snippet belongs to a web service rather than a simple console code??? – Anubhav Ranjan Aug 11 '11 at 10:49
  • There should not be. A webservice is again a serivice which is hosted in some container and that could be another console app though it doesnth have an visually console represented to user. So its basically C# and nope. – Zenwalker Aug 12 '11 at 02:15