Blue Mic

Dear all,

Somebody required that we should do validation of "new" operations' result in C#. According to MSDN, "new" operator does raise an "OutOfMemoryException" exception but I don't think this validation meaningful in C# - a programming language in which there is automatic memory management.

In case the garbage collector failed and there is no memory available, is it meaningful to do any more exception handling




Re: Visual C# General Is it necessary to check "null" after "new" operations?

C#abe

Blue Mic wrote:
Dear all,

In case the garbage collector failed and there is no memory available, is it meanful to do any more exception handling

It's ok, compiler would help you to eliminate redundant code.






Re: Visual C# General Is it necessary to check "null" after "new" operations?

ThE_lOtUs

In my opinion, it's would be important to catch this exception if you are planning of allocating a huge amount of memory (like an image editor where the image can be very big). For small amount of memory that would not be necessary. I always have a try catch block that incorporate all of the application, if anything weird is throwned, this will catch it all, show a nice error message and save the error in a log file (there's some events that can help you do that).




Re: Visual C# General Is it necessary to check "null" after "new" operations?

Peter Ritchie

What would you expect to be able to do in processing an out of memory exception I general there's not much you can do, other than let the CLR display a message and exit the application.




Re: Visual C# General Is it necessary to check "null" after "new" operations?

OmegaMan

Blue Mic wrote:
Somebody required that we should do validation of "new" operations' result in C#. According to MSDN, "new" operator does raise an "OutOfMemoryException" exception but I don't think this validation meaningful in C# - a programming language in which there is automatic memory management.

In case the garbage collector failed and there is no memory available, is it meanful to do any more exception handling


(IMHO) Note the object may be a valid object created from the new, (not null) but depending on the operations within it, it may be invalid. For example if you have a dataset which has been created, the object has been created, but it may not have tables, or columns or rows....

When dealing with an object it is best to handle different scenarios such as

Code Snippet

Dataset ds;

if (ds != null)
if (ds.Tables.count > 0)
if (ds.Tables[0].Rows.Count > 0)

...



with that said, its just good practice to put in those checks before operating on any object. The failure of creating memory is very low...but if you have checks in to handle the improbables, your code will allow you to debug those items in a better fashion, than receiving the object referenced is null.





Re: Visual C# General Is it necessary to check "null" after "new" operations?

jschell

Blue Mic wrote:
Dear all,

In case the garbage collector failed and there is no memory available, is it meanful to do any more exception handling

That would depend on the application.

If the application is attempting to load a medical media image which is 1900 meg in size and it fails due to memory then the application can certainly proceed since even though 1900 meg is not available certainly something is (or one wouldn't have gotten to that point.)

Something similar is true if one attempts to load 1,900 files into memory each of which takes 1 meg in size. A failure there could be mean unloading all the files or just not loading the last one (or perhaps the last 100.)

On the other hand if it is thrown when one attempts to allocate an int array of size one (or zero) then doing anything at all is probably a risk.

In general one shouldn't attempt to do anything but if one knows that large memory operations will be used then the requirements/design should take that into account and decide what the proper action should be.





Re: Visual C# General Is it necessary to check "null" after "new" operations?

Peter Ritchie

Another thing to consider is you may not be able to check for null. The new operation will never return null so if you have something like the following:

try

{

MyType myVariable = new MyType();

}

catch(...)

{

//...

}

Then myVariable will never be scope should new fail (e.g. if you catch any exceptions you can't access myVariable). You would have to write code like this:

MyType myVariable = null;

try

{

myVariable = new MyType();

}

catch(...)

{

//...

}

If that's the type of thing someone has required then you end up with two assignments instead of one; which is less performant. Not a good idea in my opinion; I would question the logic behind a "requirement" like that.






Re: Visual C# General Is it necessary to check "null" after "new" operations?

Blue Mic

>> there's not much you can do, other than let the CLR display a message and exit the application.
Yes, I agree with this idea. That's why I think it's not necessary.