tags:

views:

945

answers:

5

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);
}
+11  A: 

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.

Talljoe
I agree. InvalidArgument should be for a path that's invalid, not just missing. So "c::\x.txt" might be invalid but "c:\x.txt" isn't, regardless of whether it exists.
paxdiablo
That's a tricky one.... the directory could have been deleted inbetween the developer performing the check and passing the directory path to the method, so it can never really be a bug for a method to be passed a non-existant directory as there's no real way to ensure it'll still be there once the method is called.
Rob
+1  A: 

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
Rob
+1  A: 

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);
}
James
But if the parameter is null, then an ArgumentNullException would be more fitting, wouldn't it?
Svish
Yeah ArgumentNullException would be more fitting, I was just trying to use the Exceptions you opted for in the best case scenario. However, I suppose ArgumentException would be most appropriate if the directory entered was invalid i.e. Pax's comment on Talljoe's answer. Anyway, glad you got the issue resolved!
James
A: 

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.

jerryjvl
I'll readily admit though that this distinction is probably more subtle than anyone will care about in the real world ;)
jerryjvl
A: 

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();
  }
}
BeowulfOF