Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use try catch blocks in a value returning method?

Tags:

c#

asp.net

I am checking the uploaded image in a registration form , where i need to use try catch blocks. here is my code:

public bool CheckFileType(string FileName)
{
        string Ext = Path.GetExtension(FileName);
        switch (Ext.ToLower())
        {
            case ".gif":                   
                return true;
                break;
            case ".JPEG":                    
                return true;
                break;
            case ".jpg":                  
                return true;
                break;
            case ".png":                   
                return true;
                break;
            case ".bmp":                   
                return true;
                break;
            default:                  
                return false;
                break;
        }

}

please suggest me how to use the try catch blocks here.

thanks in advance.

like image 853
sreenu Avatar asked Apr 27 '11 11:04

sreenu


2 Answers

It would be better to do it this way,

 public bool CheckFileType(string FileName)
 {
    bool result = false ;

    try
     {
      string Ext = Path.GetExtension(FileName);
      switch (Ext.ToLower())
      {
        case ".gif":                   
        case ".JPEG":                    
        case ".jpg":                  
        case ".png":                   
        case ".bmp":                   
            result = true;
            break;
       }

      }catch(Exception e)
      {
         // Log exception 
      }
      return result;
     }
like image 63
Furqan Hameedi Avatar answered Oct 31 '22 05:10

Furqan Hameedi


There are plenty of ways that you can use exceptions in methods that return values:

Place your return statement outside the try-catch For example:

T returnValue = default(T);
try
{
    // My code
}
catch 
{
    // Exception handling code
}
return returnValue;

Put a return statement inside your catch

try
{
    // My code
}
catch 
{
    // Handle exception
    return default(T);
}

Throw an exception

You don't have to return a value, the method simply has to end (e.g. reach a return statement or a throw statement). Depending on the exception its not always valid to return a value.

You should think carefully about when and how to catch and handle exceptions:

  1. What might fail?
  2. Why / how can they fail?
  3. What should I do when they fail?

In your case:

  1. The only statement that can fail is string Ext = Path.GetExtension(FileName);, which according to the documentation can fail if FileName contains. (Note that GetExtension doesn't return null, even if FileName is null).
  2. This might happen if the user supplied a string that contains these invalid characters.
  3. If this happens, I guess that we should return false, to indicate that the path is not valid (however this depends on the application).

So I'd probably handle exceptions like this:

public bool CheckFileType(string FileName)
{
    string Ext;
    try
    {
        Ext = Path.GetExtension(FileName);
    }
    catch (ArgumentException ex)
    {
        return false;
    }
    // Switch statement
}

Note that we only catch the exception that we are expected (ArgumentException), and we only place the try statement around the statement that we expect the exception to be thrown from.

In fact its a good idea to avoid throwing and catching exceptions wherever possible - not only do they incur a performance penalty (which can cause serious problems if this method is called inside a loop), but you might inadvertently catch and handle an exception that you didn't anticipate, masking a more serious problem.

In this case we can avoid throwing the exception entirely by checking ourselves to see if FileName contains any invalid characters:

public bool CheckFileType(string FileName)
{
    if (FileName == null)
    {
        return false;
    }
    if (FileName.IndexOfAny(System.IO.Path.GetInvalidPathChars()) >= 0)
    {
        return false;
    }
    // Your original method goes here
}
like image 40
Justin Avatar answered Oct 31 '22 05:10

Justin