Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throwing an exception of the proper type

Tags:

c#

.net

exception

In my code I have often situations like this:

public void MyMethod(string data)
{
    AnotherClass objectOfAnotherClass = GetObject(data);
    if (objectOfAnotherClass == null)
        throw new WhatExceptionType1("objectOfAnotherClass is null.");

    if (objectOfAnotherClass.SomeProperty < 0)
        throw new WhatExceptionType2("SomeProperty must not be negative.");
}

Imagine that GetObject makes use of some external libraries which are not under my control and that this library returns null if no object for data exists and considers a negative SomeProperty as a valid state and therefore doesn't throw an exception. Further imagine that MyMethod cannot work without objectOfAnotherClass and does not make sense with a negative SomeProperty.

What are the proper exceptions for WhatExceptionType1/2 to throw in this situation?

Basically I had four options in mind:

  • 1) InvalidOperationException, because MyMethod doesn't make sense under the conditions described above. On the other hand the guidelines (and Intellisense in VS too) say that an InvalidOperationException should be thrown if the object the method belongs to is in an invalid state. Now the object itself isn't in an invalid state. Instead the input parameter data and some other operations based on this parameter lead to a situation where MyMethod cannot operate anymore.

  • 2) ArgumentException, because there are values for data the method can work with and other values the method can't. But I cannot check this by inspecting data alone, I have to call other operations before I decide.

  • 3) Exception, because I don't know which other exception type to use and because all other predefined exceptions feel too specialized and not fitting to my situation.

  • 4) MyCustomException (my own exception type derived from Exception). This seems always an option but I am worried that I have to define lots of special exception classes for many different error conditions when I start to follow this pattern.

Are there other and better options? What are the arguments in favor or against those options?

Thank you for feedback in advance!

like image 398
Slauma Avatar asked Jan 05 '11 15:01

Slauma


People also ask

How do you throw an exception correctly?

Throwing an exception is as simple as using the "throw" statement. You then specify the Exception object you wish to throw. Every Exception includes a message which is a human-readable error description. It can often be related to problems with user input, server, backend, etc.

What does throwing an exception mean?

The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred. Creating an exception object and handing it to the runtime system is called throwing an exception.

What type of exception is thrown?

We can throw either checked or unchecked exceptions in Java by throw keyword. It is mainly used to throw a custom exception. We will discuss custom exceptions later in this section.

When should a method throw an exception?

In short: You should throw an exception if a method is not able to do the task it is supposed to do.


1 Answers

If built-in exceptions exist that make sense, I would use those. If not, it makes sense to roll your own exception -- even if it's an empty class that extends Exception -- because this allows you to detect specific exception types. If you just threw Exception, for example, how do you know the exception was because objectOfAnotherClass was null, and that it wasn't some exception raised in GetObject?

So to summarize: specific exceptions are better, because you can (potentially) diagnose and recover from specific cases. Hence, use the built-in .NET exceptions (if they're sufficient), or else roll your own exceptions.

Edit: I should clarify that I rarely use existing exceptions and put a message in them. It makes your code more readable if the exception type tells you the error, rather than having to debug, generate the exception, and then inspect the message to see what the problem is.

like image 80
ashes999 Avatar answered Oct 17 '22 20:10

ashes999