LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-20-2008, 02:30 PM   #1
lindylex
Member
 
Registered: Mar 2007
Posts: 192

Rep: Reputation: 17
why not use try and catch?


I was recently asked why it is not a good idea to use try and catch (e) when developing some web applications and I had no idea. Does anyone have a good idea why you might not want to use this when developing some web applications?

Lex
 
Old 01-20-2008, 02:37 PM   #2
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Rep: Reputation: 53
You should deal with exceptions anyway, so I can't see what's wrong in try and catch.
 
Old 01-20-2008, 04:40 PM   #3
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by lindylex View Post
Does anyone have a good idea why you might not want to use this when developing some web applications?
If you are making calls that might throw exceptions, and you do not handle them yourself, the default exception handler will be called. Since you do not have control over what the default exception handler does, it is a bad programming practice to not create your own exception handler that can gracefully recover from the problem. The person who gave you this advice might be a lazy programmer.
 
Old 01-21-2008, 04:54 PM   #4
lindylex
Member
 
Registered: Mar 2007
Posts: 192

Original Poster
Rep: Reputation: 17
Thanks for the insight all.

Lex
 
Old 01-21-2008, 05:23 PM   #5
boughtonp
Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 32

Rep: Reputation: 1
Two situations for not using try/catch spring to mind...

When you do have control over the default exception handler, it can sometimes be better to let an error fall to the bottom, so that the error is handled consistantly. (When that makes sense depends on the type of error and type of web application.)

Also, if you are continuously using try/catch as part of standard program flow, it may well be slowing things down by generating overhead of the error details, which could be avoided by using appropriate verification functions instead.

Consider:
try {a=b+1} catch {a=1}
versus:
if isNumeric(b) {a=b+1} else {a=1}
 
Old 01-21-2008, 07:10 PM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177
Exceptions are a very good way to deal with ... well ... exceptions to the normal rule. This can be a programming error, a case that "should never happen but just did," or even the user wishing to bail-out what he started doing.

When you know you can "throw the panic-ball up into the air and somebody back there will be prepared to catch it," you avoid littering your code with a bunch of tests of return-codes. The code that detects the unlikely-condition responds by throwing an exception instead of returning a code.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to catch events nick021 Linux - Software 2 04-07-2006 08:24 PM
The grub catch 22 Devyn Linux - Software 21 11-19-2005 10:39 PM
try catch(...) Sinner6 Linux - Newbie 3 06-25-2004 10:22 AM
Catch signals C++ moyacuba Programming 2 03-18-2004 01:28 PM
Well...I'll catch you all later... Whitehat General 30 03-11-2004 10:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration