Why create custom exceptions?

Why do we need to create custom exceptions in .NET?

+47
c # exception
Jan 6 '09 at 17:26
source share
11 answers

Specific customs exceptions allow you to separate different types of errors for your statements. The general design for handling exceptions is as follows:

 try {} catch (Exception ex) {} 

Captures all exceptions regardless of type. However, if you have custom exceptions, you can have separate handlers for each type:

 try {} catch (CustomException1 ex1) { //handle CustomException1 type errors here } catch (CustomException2 ex2) { //handle CustomException2 type errors here } catch (Exception ex) { //handle all other types of exceptions here } 

Ergo, specific exceptions allow you to control the level of exception handling. This benefit extends not only to custom exceptions, but to all other types of exceptions in the .NET system libraries.

+48
Jan 06 '09 at 17:34
source share

Recently, I have made a long blog on this subject:

http://blogs.msdn.com/jaredpar/archive/2008/10/20/custom-exceptions-when-should-you-create-them.aspx

Its essence is as follows: create your own exception if one of the following values:

  • You really expect someone to handle this.
  • You want to log information about a specific error.
+35
Jan 06 '09 at 17:33
source share

So, you can also drop them yourself, and then catch them and know exactly what they mean.

Also: if you create a class library / framework / api, it is often useful to create a BaseException, which is why other exceptions in your code are inherited. Then, when your code throws exceptions, programmers who use it can quickly find out the source of the exception.

+18
Jan 06 '09 at 17:28
source share

Because it can make your intentions understandable, and you can also track usage using the functionality of the IDE. Say you have a custom backend system called "FooBar" and you create a "FooBarDownException", you can track the use of this exception to identify any user logic that contains your application because FooBar does not work. You can choose this particular type of exception and ignore others, avoiding overloads and conditional logic in exception handlers. This is really just another version of strong typing. It also means that you can avoid comments in your code because the exception has a drop-down name of intent.

+9
Jan 6 '09 at 17:29
source share

I'm not sure why it is β€œtechnically”, but it allows me to say that I have an application / website that uses permissions. If someone does not have the correct permission, his stupidity is to throw a DivideByZero exception or an IOException. Instead, I can throw my AccessDeniedException, which will help me debug later.

+3
Jan 06 '09 at 17:28
source share

For the same reason, you would create different exit codes for a non-.NET application: to indicate different errors depending on a particular program. Like ... ConnectionBrokenException or um ... UserSmellsBadException ... or something.

That way, you can know exactly what went wrong and act accordingly. For example, if you try to send some data and the transfer class selected ConnectionBrokenException , you can open the reconnect dialog box and try to reconnect. Then the reconnection method will throw a ConnectionTimeoutException if it expires and you can act accordingly again.

+3
Jan 6 '09 at 17:31
source share

As Joel wrote: β€œYou can also drop them yourself, and then catch them and know exactly what they mean.

In addition, you can add specific information about the problem to allow the exception handler to act more accurately.

+2
Jan 6 '09 at 17:32
source share

Another reason when a client is talking to interfaces. Since the client does not know about the implementation of the interface, and since they may be able to use different exceptions, this is a good place to create custom exceptions to unify the errors that occur.

I wrote about this particular case:

http://blog.mikecouturier.com/2010/01/creating-custom-exceptions-in-net-right.html

+2
Mar 23 '11 at 4:24
source share

.NET standard exceptions do not cover all the bad that can go wrong in any application, and they are not intended for this. If your program is not very simple, you will probably have to create at least a few custom exceptions.

+1
Jan 06 '09 at 17:31
source share

On the one hand, Exceptions are implemented in the Library, not in the language - how can they create exceptions in the library? I am sure that you are not upholding that system libraries should have a different set of rules.

Otherwise, you can actually use the tree of exception objects. Inherited exceptions can have special attributes, if you want - they can be used for more complex things than they are. I am not a supporter of their use as a general mechanism for transmitting data or something else (although they may be), but I could see a case where someone implemented their own logging solution that required a special attribute in Exception ...

Your custom exceptions may contain a flag indicating a special appeal (perhaps one message that you should restart the JVM), they may contain information about logging levels, a bunch of things.

In any case, I am not a supporter of this, I just say that this is possible. The first paragraph is the real answer.

0
Jan 06 '09 at 17:36
source share

You should not if the built-in exceptions correctly describe the problem / exception. I would not create my own base classes to create custom ArgumentException , ArgumentNullException or InvalidOperationException .

You can create your own exceptions and describe the error at a higher level. however, this usually does not help in debugging from the consumer class.

If you select and catch the exception yourself, you may need a custom exception.

0
Jan 11 '09 at 15:58
source share



All Articles