Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2004

Tip: Looking for answers? Try searching our database.

Throws vs Try & Catch

Thread view: 
Khoa Tran - 14 Apr 2004 23:55 GMT
Could anyone explain to me the main advantages of using Try and Catch rather
than Throwing Exceptions.

For convinience, I often throws exception but in most elegantly written
programs, people almost always use Try and Catch.

Thank you.

--
Khoa Tran
Chris Smith - 15 Apr 2004 00:24 GMT
> Could anyone explain to me the main advantages of using Try and Catch rather
> than Throwing Exceptions.
>
> For convinience, I often throws exception but in most elegantly written
> programs, people almost always use Try and Catch.

This isn't a comparison that can be made.  The two language constructs
do different things.  You should use try and catch when you want to
handle an error.  You should use throws when you want to declare that
someone else needs to handle an error.

Signature

www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Roedy Green - 15 Apr 2004 04:51 GMT
>Could anyone explain to me the main advantages of using Try and Catch rather
>than Throwing Exceptions.

It is a matter of who knows best what to do when something goes wrong.
IF you can fix the problem, e.g. restart the connection, then you
should handle the exception with a catch.  If you can't, generally you
should pass the exception on up.  Sometimes you do both, catch, do
something to handle or report the exception, then throw ANOTHER
exception, (with a higher level message attached to it describing in
more application-friendly language what went wrong).

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Bryce (Work) - 15 Apr 2004 17:31 GMT
>Could anyone explain to me the main advantages of using Try and Catch rather
>than Throwing Exceptions.
>
>For convinience, I often throws exception but in most elegantly written
>programs, people almost always use Try and Catch.

Generally, I try to only have my method throw when its not possible
for my method to recover gracefully from an exception.

I use try, catch when I can recover from the exception...,  For
example, if you want to retry a database connection after one failed,
or you are doing math, and want to catch div by zero by just passing
back zero...

I try to also throw exceptions that mean something in its context. If
you are seperating your business logic from your data access, then you
might not want to have to catch SQLExceptions in your business logic.
In my data access logic, I capture SQLExceptions and convert to my
own.. YMMV, and its a preference thing (sometimes mandated by code
standards)

--
now with more cowbell
Mark Haase - 15 Apr 2004 19:39 GMT
> Could anyone explain to me the main advantages of using Try and Catch rather
> than Throwing Exceptions.
>
> For convinience, I often throws exception but in most elegantly written
> programs, people almost always use Try and Catch.

They're just two sides of the same coin.

For instance, notice that any code which can produce a *Checked*
Exception (as opposed to Unchecked) must eventually be bracketed in a
try-catch. Maybe not in its original method, but the method that calls
that method. Or maybe the try-catch block is 10 calls away in the stack
trace. At some point, somebody has to use try-catch to ensure that the
error gets handled.

The only reason for having the term "throws" in the java language is
that you, as the designer of a class, can decided *where* the try-catch
block should go. If an error occurs and its something you can handle
inside your class, then the try-catch should be inside your class.

On the other hand, if you're building a package for other developers to
use, there are probably going to be many instances where you want *them*
to write out the try-catch block in order to let them know when they've
made mistakes with the API. Thats when you would designate your methods
to throw a certain exception.

|\/|  /|  |2  |<
mehaase(at)sas(dot)upenn(dot)edu


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.