Products

Solutions

Resources

Partners

Community

Blog

About

QA

Ideas Test

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeArchived Discus...Archived Discus...Developing Under Previous Versions of .NETDeveloping Under Previous Versions of .NETASP.Net 2.0ASP.Net 2.0Is "Try Catch" slow?Is "Try Catch" slow?
Previous
 
Next
New Post
6/23/2008 4:04 PM
 

And just my 2 cents to add to Charles... programming with try catch as FUNCTIONALITY is bad practice.  In other words, I'll use an example: In the .NET 1.x world lets say you wanted to convert some string data to a numeric but you had no way of knowing if the data coming in is actually representable by a number.  You could use the Convert.ToInt32(someString) (returns 0 if it can, might throw an exception though if the string is way out of whack) or maybe you could have written something like try { someNum = Int32.Parse(someStringRepresentation); } catch (System.Exception ex) { // logic here } (and as somebody mentioned you'd actually not use the general System.Exception class, but something more specific).  While this is "ok", if you try to run this same operation on thousands of strings and a good portion of them are failing, you'll see this has a huge performance hit.  So as Charles mentioned, you'd want to do something to check and make sure it'd work.  .NET 2.0 added a TryParse method which basically uses Regex or some other method to check before attempting to parse, increasing the speed and performance drastically.

Try/Catch should only be used to catch REAL exceptions.  In other words, something could possibly happen here out of the norm such as an invalid IO operation (network drive lost, maybe?)... not that my bad coding is going to produce errors.  Embedding an entire huge piece of functionality in a try/catch is lazy and irresponsible of a programmer -- you should seek out, as Charles suggested, the root of the problem and prevent those errors from being there in the first place.


-- Jon Seeley
DotNetNuke Modules
Custom DotNetNuke and .NET Development
http://www.seeleyware.com
 
New Post
6/27/2008 6:18 AM
 

Brandon

hmm, its a bit confused

"Finally, don't wrap your entire method in one big Try-"Catch Exc as Exception" monster.  I know of one unnamed awesome application that does this all the time, and it drives me crazy!"

In generally we were making it very often :( Can you explain a bit more 2 things:

  1.  Why we do not need to use "System.Exception". Could be we do not know what type of exception will be?
  2. Why we do not need to use single Try Catch per control (like "bubble " as Cathal has said)?

Thank you for your help!

BTW: No problems with httpModule now. Everything works very fast. We had refactored our code. Also we do not use Try Catch at all. Only code validators like "if(obj==null)" or something like this.

Sergey

 
New Post
6/27/2008 6:27 AM
 

Charles && Fooberichu

Thank you very much for your comments. In generally problem has been solved. We did not use Try Catch, all verifications are in the code. We do not want to make more bad code (there are a lot already :( we have to fix it everyday from different clients from old developers :(

Thank you for your help.

Only one question still opened for Brandon is about "System.Exception" - why we should not use it?

Sergey

 
New Post
6/27/2008 7:18 AM
 

Sergey,

It is a good practice to be explicit about the types of exceptions you expect to catch so that you can have specific logic for those cases.  Then you can have a fallback exception handler for those cases where you don't know which exception will be thrown.  This means that instead of using a simple try/catch, use multiple catch statements:

try
{
    //Do some work
}
catch (System.IO.FileLoadException fex)
{
    //What to do if I can't load the file
}
catch (Exception ex)
{
    //Not sure what happened
}


Joe Brinkman
DNN Corp.
 
New Post
7/5/2008 7:02 AM
 

Joe

Super! Thank you very much! Its really cool example. I like it a lot and will use in future.

Thank you

Sergey

 
Previous
 
Next
HomeHomeArchived Discus...Archived Discus...Developing Under Previous Versions of .NETDeveloping Under Previous Versions of .NETASP.Net 2.0ASP.Net 2.0Is "Try Catch" slow?Is "Try Catch" slow?


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out