In our professor's example code he has one snippet that looks like this:
if (name == null || name == "")
throw new ArgumentException("name is null or empty");
And another snippet that looks like this:
if (!File.Exists(name))
{
throw new Exception("File does not exist!");
}
I was just wondering what the different was and why one is used above the other
TLDR: ArgumentException is a type of Exception used to provide more detailed info object is the root of all things. There are many subtypes of object. Exception is one of them. There are many subtypes of Exception.
Exception is the base class for all exceptions. ArgumentException is used to say that a parameter is not valid. It subclasses from Exception. With catch, you can actually filter base on the type of the exception and handle each one differently.
This is the main difference between the two words. The word ‘exemption’ is used as a noun, and it is formed out of the verb ‘exempt’. On the other hand, the word ‘exception’ is used as a noun and it is formed out of the verb ‘except’. Take a look at the following sentences. 1. Francis got an exemption in the first year of his post graduate course.
Java.lang.error is the package that contains the error class, whereas java.lang.Exception is the package that contains the exception class. Error can not be recovered, so it is irrecoverable. This is a con of the occurrence of errors. On the other hand, Exceptions can be recovered and handled.
Exception
is the base class for all exceptions. ArgumentException
is used to say that a parameter is not valid. It subclasses from Exception
. With catch
, you can actually filter base on the type of the exception and handle each one differently.
MSDN describes it well:
When you have to throw an exception, you can often use an existing exception type in the .NET Framework instead of implementing a custom exception. You should use a standard exception type under these two conditions:
- You are throwing an exception that is caused by a usage error (that is, by an error in program logic made by the developer who is calling your method). Typically, you would throw an exception such as ArgumentException, ArgumentNullException, InvalidOperationException, or NotSupportedException. The string you supply to the exception object's constructor when instantiating the exception object should describe the error so that the developer can fix it. For more information, see the Message property.
- You are handling an error that can be communicated to the caller with an existing .NET Framework exception. You should throw the most derived exception possible. For example, if a method requires an argument to be a valid member of an enumeration type, you should throw an InvalidEnumArgumentException (the most derived class) rather than an ArgumentException.
Exception
is a base class. It is just the most generic kind of exception. In many cases, a more specific type can be used to give more information about the kind of error that occurred. In this case, ArgumentException
is a type of exception that indicates there was an error with the arguments.
TLDR: ArgumentException
is a type of Exception
used to provide more detailed info
object is the root of all things.
There are many subtypes of object. Exception is one of them.
There are many subtypes of Exception. SystemException is one of them.
There are many subtypes of SystemException. ArgumentException is one of them.
There are many subtypes of ArgumentException. ArgumentNullException and ArgumentOutOfRangeException are two of them.
If your professor really wanted to use the most explicit exception they could have written
if (name==null) throw new ArgumentNullException("name");
if (name=="") throw new ArgumentOutOfRangeException("name", name, "name cannot be zero length");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With