c# - What is the difference between ArgumentException and just Exception? -


in our professor's example code has 1 snippet looks this:

if (name == null || name == "")     throw new argumentexception("name null or empty"); 

and snippet looks this:

if (!file.exists(name)) {     throw new exception("file not exist!"); } 

i wondering different , why 1 used above other

exception base class exceptions. argumentexception used parameter not valid. subclasses exception. catch, can filter base on type of exception , handle each 1 differently.

msdn describes well:

when have throw exception, can use existing exception type in .net framework instead of implementing custom exception. should use standard exception type under these 2 conditions:

  • you throwing exception caused usage error (that is, error in program logic made developer calling method). typically, throw exception such argumentexception, argumentnullexception, invalidoperationexception, or notsupportedexception. string supply exception object's constructor when instantiating exception object should describe error developer can fix it. more information, see message property.
  • you handling error can communicated caller existing .net framework exception. should throw derived exception possible. example, if method requires argument valid member of enumeration type, should throw invalidenumargumentexception (the derived class) rather argumentexception.

Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -