Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between ArgumentException and just Exception?

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

like image 946
pkerts Avatar asked Apr 22 '16 00:04

pkerts


People also ask

What is argumentexception in Java?

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.

What is the difference between exception exception and argument 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.

What is the difference between'exemption'and'exception'?

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.

What is the difference between error and exception in Java?

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.


3 Answers

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.
like image 68
Daniel A. White Avatar answered Sep 23 '22 16:09

Daniel A. White


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

like image 34
nhouser9 Avatar answered Sep 25 '22 16:09

nhouser9


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");
like image 20
Paul Smith Avatar answered Sep 25 '22 16:09

Paul Smith