Click here to Skip to main content
15,912,756 members
Home / Discussions / C#
   

C#

 
AnswerRe: Throwing exception when a property must not be null? Pin
Paul Riley8-Sep-02 0:33
Paul Riley8-Sep-02 0:33 
GeneralRe: Throwing exception when a property must not be null? Pin
Martin Haesemeyer8-Sep-02 0:39
Martin Haesemeyer8-Sep-02 0:39 
GeneralRe: Throwing exception when a property must not be null? Pin
Paul Riley10-Sep-02 4:50
Paul Riley10-Sep-02 4:50 
GeneralRe: Throwing exception when a property must not be null? Pin
Martin Haesemeyer10-Sep-02 9:56
Martin Haesemeyer10-Sep-02 9:56 
GeneralRe: Throwing exception when a property must not be null? Pin
Paul Riley10-Sep-02 10:10
Paul Riley10-Sep-02 10:10 
AnswerRe: Throwing exception when a property must not be null? Pin
Erik Westermann9-Sep-02 7:43
professionalErik Westermann9-Sep-02 7:43 
GeneralRe: Throwing exception when a property must not be null? Pin
Paul Riley9-Sep-02 9:34
Paul Riley9-Sep-02 9:34 
GeneralRe: Throwing exception when a property must not be null? Pin
Erik Westermann10-Sep-02 4:06
professionalErik Westermann10-Sep-02 4:06 
Paul Riley wrote:
Wouldn't you say that trying to set something to null that shouldn't ever be null is an exceptional circumstance?

Have you come across the two pronged plugs that plug into the wall only one way? One prong of the plug is keyed and so is the wall receptacle so that the prong must match the wall receptacle's orientation - this is done to prevent something from being plugged in backwards.

The person that designed the plug expected that someone would eventually try to put the plug into the receptacle the wrong way and took measures to ensure the chances of that happening are much smaller than if he did not design the plug with that possibility in mind. My point is that the plug's designer knew that this could be a problem and wants to prevent the device that's being plugged in from working when the plug is put in the wrong way - this is obviously an expected condition.

I view this scenario the same way - since it is possible to set a property to null and the class requires a non-null value - this is nothing exceptional.

Paul Riley wrote:
Why would you do that if one already exists that meets the criteria you're looking for?

Simply becasue the idea of the .NET Platofrm is to provide a space where all activities occur in a managed, type-safe environment. The System namespace is called "system" becuase it exposes classes that you can use to interact with the "system". If your applicaiton throws an exception, throw an applicaiton-specific exception that's derived from System.ApplicationException to avoid confusion with CLR-generated exceptions.

Moreover, CLR-based exceptions usually need to be dealt with in another way than do application-specific exceptions. For example, if the CLR throws an OutOfMemoryException, you'll have to deal with that differnetly than if an application throws it's own memory-specific exception. In the application's case, the problem could be the result of some fixed capacity buffer becomming full whereas the CLR-based exception obviously indicates a problem that affects the entire system.

Given that the System.ApplicationException class is a derivitave of System.Exception, a catch block that catches a System.Exception type will also catch System.ApplicationException exceptions, thus reducing the chance that an applicaiton-specific exception could be missed.

Erik Westermann
Author, Learn XML In A Weekend (October 2002)
GeneralRe: Throwing exception when a property must not be null? Pin
Paul Riley10-Sep-02 4:47
Paul Riley10-Sep-02 4:47 
GeneralRe: Throwing exception when a property must not be null? Pin
Erik Westermann10-Sep-02 15:37
professionalErik Westermann10-Sep-02 15:37 
GeneralRe: Throwing exception when a property must not be null? Pin
Paul Riley11-Sep-02 1:23
Paul Riley11-Sep-02 1:23 
GeneralRe: Throwing exception when a property must not be null? Pin
leppie11-Sep-02 6:32
leppie11-Sep-02 6:32 
GeneralRe: Throwing exception when a property must not be null? Pin
Paul Riley11-Sep-02 7:04
Paul Riley11-Sep-02 7:04 
GeneralRe: Throwing exception when a property must not be null? Pin
leppie11-Sep-02 7:44
leppie11-Sep-02 7:44 
GeneralRe: Throwing exception when a property must not be null? Pin
Paul Riley11-Sep-02 13:26
Paul Riley11-Sep-02 13:26 
GeneralRe: Throwing exception when a property must not be null? Pin
leppie11-Sep-02 16:37
leppie11-Sep-02 16:37 
GeneralRe: Throwing exception when a property must not be null? Pin
Paul Riley11-Sep-02 23:43
Paul Riley11-Sep-02 23:43 
GeneralRe: Throwing exception when a property must not be null? Pin
leppie12-Sep-02 2:08
leppie12-Sep-02 2:08 
GeneralRe: Throwing exception when a property must not be null? Pin
Paul Riley12-Sep-02 2:52
Paul Riley12-Sep-02 2:52 
GeneralRe: Throwing exception when a property must not be null? Pin
leppie12-Sep-02 3:50
leppie12-Sep-02 3:50 
GeneralRe: Throwing exception when a property must not be null? Pin
Martin Haesemeyer10-Sep-02 10:06
Martin Haesemeyer10-Sep-02 10:06 
GeneralRe: Throwing exception when a property must not be null? Pin
Erik Westermann10-Sep-02 15:48
professionalErik Westermann10-Sep-02 15:48 
GeneralRe: Throwing exception when a property must not be null? Pin
jparsons10-Sep-02 5:07
jparsons10-Sep-02 5:07 
GeneralRe: Throwing exception when a property must not be null? Pin
stephen woolhead13-Sep-02 10:48
stephen woolhead13-Sep-02 10:48 
GeneralAdjusting colors Pin
Nnamdi Onyeyiri7-Sep-02 23:49
Nnamdi Onyeyiri7-Sep-02 23:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.