Throw exception from Called function to the Caller Function's Catch Block
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:
- when you want to handle the exception.
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.
Just use throw in the called function. Dont overload catch blocks with multiple exception types. Let the caller take care of that.