Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 01-20-2008, 02:30 PM   #1
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?

Old 01-20-2008, 02:37 PM   #2
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
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
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
Registered: Mar 2007
Posts: 192

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

Old 01-21-2008, 05:23 PM   #5
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.

try {a=b+1} catch {a=1}
if isNumeric(b) {a=b+1} else {a=1}
Old 01-21-2008, 07:10 PM   #6
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.


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 > Forums > Non-*NIX Forums > Programming

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

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