We often use try catch statements in our method, if the method can return a value, but the value doesn’t a string, how to return the exception message?
For example:
public int GetFile(string path)
{
int i;
try
{
//...
return i;
}
catch (Exception ex)
{
// How to return the ex?
// If the return type is a custom class, how to deal with it?
}
}
How to return the exception?
You can
removetry catch block to throw exception orthrowexception from catch block if you want to do something useful in catch block like logging the exception. If you want to send exception message from your method and do not want to throw exception then you can use out string variable to hold the exception message for calling method.How to call the method.