Svish
Svish

Reputation: 158181

C#: Should I throw an ArgumentException or a DirectoryNotFoundException?

I have a method which takes a directory path as a string. In the beginning of the method it checks if this path exists and if not it should throw an exception. I'm thinking it should maybe throw a DirectoryNotFoundException or something instead of a less specific ArgumentException.

I read the msdn documentation of that DirectoryNotFoundException and it says that

DirectoryNotFoundException uses the HRESULT COR_E_DIRECTORYNOTFOUND which has the value 0x80070003.

I don't know what that means exactly, and it looks a bit scary... should I still throw that exception, or should I stick to a regular ArgumentException? Or should I stick to the ArgumentException simply because it is an argument I am complaining about? Or?

public void MakeFunOf(string path)
{
    if(!Directory.Exists(path))
        throw new WhatException();
    TellJokeAbout(path);
    PointAndLaughAt(path);
}

Upvotes: 14

Views: 14164

Answers (5)

Oliver Friedrich
Oliver Friedrich

Reputation: 9250

In my opinion you should check for the correctnes of the argument and throw an ArgumentException then after the check throw an DirectoryNotFoundException. It is a big difference if no argument was given or only a wrong path was specified.

void CheckDir(string path)
{
  if(String.IsNullOrEmpty(path))
  {
    throw new ArgumentException("Path not specified.");
  }
   if(!Directory.Exists(path))
  {
    throw new DirectoryNotFoundException();
  }
}

Upvotes: 2

jerryjvl
jerryjvl

Reputation: 20151

Referring to the documentation of 'ArgumentException':

ArgumentException is thrown when a method is invoked and at least one of the passed arguments does not meet the parameter specification of the called method. All instances of ArgumentException should carry a meaningful error message describing the invalid argument, as well as the expected range of values for the argument.

To the letter this means that the exception choice depends on the specification/documentation of your method.

If the path parameter is documented as something like 'Path to an existing file/directory' then you would be justified in throwing an 'ArgumentException' (or derivative) because basically by virtue of the documentation you have made the caller responsible for ensuring the file is actually there.

If the path parameter is documented more generally as 'Path to a file to joke about and laugh at', then I'd say 'DirectoryNotFoundException' is more appropriate.

Upvotes: 0

James
James

Reputation: 82136

Sounds like you should be throwing a DirectoryNotFoundException as ArgumentException is put to better use if the specified parameter is not provided....i.e. its null.

Another option is to create your own exception and throw that instead i.e.

[Serializable]
public class InvalidConfigurationException: Exception
{
    public InvalidConfigurationException() : base()
    {
    }

    public InvalidConfigurationException(string message)
        : base(message)
    {
    }

    public InvalidConfigurationException(string message, Exception innerException)
        : base(message, innerException)
    {
    }

    protected InvalidConfigurationException(SerializationInfo info, StreamingContext context) 
        : base(info, context) 
    { 
    }
}

Then you could do:

public void MakeFunOf(string path)
{    
   if(!Directory.Exists(path))        
       throw new InvalidConfigurationException('Directory entered was invalid or does not exist');
   TellJokeAbout(path);    
   PointAndLaughAt(path);
}

Upvotes: 1

Rob
Rob

Reputation: 45779

It's just my opinion (as I have nothing concrete to back it up), but here are my reasons for throwing DirectoryNotFoundException rather than ArgumentException:

  • You should throw the most specific/accurate type of Exception that you can to allow the consumer of your code to understand the reason for throwing the exception.
  • Given that Framework methods will throw a DirectoryNotFoundException when you try and do something with a directory that's not present, rather than an ArgumentException, follow the bejaviour of the Framework

Upvotes: 1

Talljoe
Talljoe

Reputation: 14827

If you expect the developer to check for the existence of the directory before calling your method, use ArgumentException. If you want the developer to have the choice of handling the missing directory, use DirectoryNotFound exception.

In other words, "Is it a bug that the developer told me to access a directory that didn't exist?"

Personally, I'd use the DirectoryNotFound exception.

Upvotes: 16

Related Questions