.NETGURU
try/catch vs. if null this else this
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngspeed' list.


Moore, Dot Net Guruhew
-- Copied from [aspngcs] to [aspngspeed] by Cain Marco <Click here to reveal e-mail address> --

What are the performance recommendations for checking if something is null before doing something with it or doing a try catch? I was doing something like the following, but it looked like lots of branches and I don't know much about how the try/catch works to compare which is better. I don't care whether the string is null, but if it is, what goes on next.

Any recommendations or people done benchmarking on this?

Thanks
Dot Net Guru

Pseudocode:

If (string != null)
    ThisGuy = string;
Else
    ThisGuy = OtherString;

-----------------------------
vs.
-----------------------------

try
{
    ThisGuy = string;
}
catch
{
    ThisGuy = OtherString;
}

Reply to this message...
 
    
Little, Ambrose
I'm no expert, but I'd say that checking for null would be faster because
throwing an exception requires the creation of a new exception object.

--Ambrose

[Original message clipped]

******************************************************************************
The Company reserves the right to amend statements
made herein in the event of a mistake. Unless expressly
stated herein to the contrary, only agreements in writing signed
by an authorized officer of the Company may be enforced against it.
*******************************************************************************

Reply to this message...
 
    
Stefan Demetz
using exceptions for business logic is both bad
practice(despite that it is convenient in VB) and requires
lots of resources

----- Original Message -----
From: "Moore, Dot Net Guruhew" <Click here to reveal e-mail address>
To: "aspngspeed" <Click here to reveal e-mail address>
Sent: Tuesday, May 28, 2002 5:36 PM
Subject: [aspngspeed] try/catch vs. if null this else this

> -- Copied from [aspngcs] to [aspngspeed] by Cain Marco
<Click here to reveal e-mail address> --
[Original message clipped]

like the following, but it looked like lots of branches and I don't know
much about how the try/catch works to compare which is better. I don't care
whether the string is null, but if it is, what goes on next.
[Original message clipped]

Reply to this message...
 
    
Jason Salas
Dot Net Guru,

You're right about using an If...Then construct to save on server resources.
Using an Try...Catch...Finally structure is better in some cases (like with
connecting to and displaying records in a database, in which scenario a
custom error mesage could be displayed if thr DB has no records in it or ran
into some sort of error). However, to get the most performance out of your
Web app, several ASP.NET texts recommend not using Try...Catch...Finally if
you don't have to.

In the code you presented as an example (testing if a string was null), the
If...Then...Else situation would yield a faster response. If you needed to
test against a database field being null, you might want to consider a Try
statement.

Good luck!

Jason

---------------------------------------------------
Jason Salas, MBA, MCP
Web Development Manager
Pacific Telestations, Inc. (dba, "KUAM")
URL: http://www.kuam.com
President, .NET User Group of Guam
URL: http://www.guam-asp.net
Mailto: Click here to reveal e-mail address

----- Original Message -----
From: "Stefan Demetz" <Click here to reveal e-mail address>
To: "aspngspeed" <Click here to reveal e-mail address>
Sent: Wednesday, May 29, 2002 8:30 AM
Subject: [aspngspeed] Re: try/catch vs. if null this else this

[Original message clipped]

Reply to this message...
 
 




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification