Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Exception to throw when a method try to use a field that can be null? [duplicate]

Tags:

I am actually working on a Framework development, which means require a really strong coding methodology.

I am facing a problem where I do not know which System.Exception derivated class I need to throw. Basically the case is when I have a class with fields that can be optionnaly initialized by the constructor and that have methods using these fields. Which exception must I throw if the user did not initialized these fields? (which means they are null)

Here is an example:

public class MyConnection {     private Uri endpointUri;      public Uri EndpointUri     {         get         {             return this.endpointUri;         }          set         {             this.endpointUri = value;         }     }      public MyConnection()     {     }      public MyConnection(Uri endpointUri)     {         this.endpointUri = endpointUri;     }      public FileStream GetFile()     {         if (this.endpointUri != null)         {             // My doer methods         }         else         {             throw new TheExceptionINeedToThrow("endpointUri", ...);         }                     } } 

Note that I have been reading the whole "Framework Design Guidelines" chapter concerning exception handling and throwing and that I did not found any solution fitting this exact case. Or maybe I misunderstood something ...

Thanks for your help.

EDIT : The fact that I provide an empty constructor seems a bit confusing regarding my problem but it is completely voluntary. In some objects that have to comply with a range of different states that cannot be duplicated in multiple objects it is sometimes useful.

like image 868
Ucodia Avatar asked Dec 14 '09 19:12

Ucodia


People also ask

How do you throw exception if it is null?

The way you have it is correct. The Null-conditional Operator only returns NULL . There's no shortcut to throw an exception but one of the things you can do is shorten checks like if (Foo == null || Foo. Bar == null || Foo.

When should you throw an ArgumentException?

ArgumentException is thrown when a method is invoked and at least one of the passed arguments does not meet the parameter specification of the called method. The ParamName property identifies the invalid argument.

What is null exception in C#?

A NullReferenceException happens when you try to access a reference variable that isn't referencing any object. If a reference variable isn't referencing an object, then it'll be treated as null .

What does Method return if exception is thrown?

Does throwing an exception within a method cause the method to return? Yes. Execution of a method is being interrupted, so method returns it execution flow.


1 Answers

Throw InvalidOperationException:

The exception that is thrown when a method call is invalid for the object's current state.

Note that the null reference isn't being passed into the method - it's already there when the method is called - so it's the object's current state which is invalid, not an argument.

However, it would be better to prevent the object from being created in this way to start with, if at all possible - does it have to be a writable property? Would you ever want an instance which did have a null endpoint URI?

like image 122
Jon Skeet Avatar answered Oct 07 '22 21:10

Jon Skeet