Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • December 05, 2016, 08:24:27 AM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Programming strategies -- dealing with exceptions (... in Java)  (Read 3655 times)

Armando

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 2,727
    • View Profile
    • Donate to Member
Programming strategies -- dealing with exceptions (... in Java)
« on: November 23, 2008, 10:57:14 PM »
I'm wondering how/when to best approach exception handling in a program.

It's not about how to technically deal with any exception in particular, but about strategies to deal appropriately with exceptions, in general.

Might sound simplistic, but my first question would be... When (at what stage of the programming phase/process) do you generally deal with exceptions?

1- As you code, you deal with the possible exceptions right away (before finishing a class, a even a small method), where they seem likely to happen,
2- or once a big block of code is written (e.g. : an entire class, or even the whole program...) then you deal with the possible exceptions...

Thanks.



« Last Edit: November 23, 2008, 10:59:10 PM by Armando »

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 36,406
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: Programming strategies -- dealing with exceptions (... in Java)
« Reply #1 on: November 24, 2008, 01:05:47 AM »
i believe that one should be planning what exceptions can be thrown and how to deal with them as you are doing class design.
it's not something that should be left for later -- there are very important decisions (which are hotly debated in the programming world) about whether you are going to use exceptions only for catastrophic errors, or for relatively-normal function returns called with out of range parameters, etc.

mahesh2k

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 1,417
    • View Profile
    • Donate to Member
Re: Programming strategies -- dealing with exceptions (... in Java)
« Reply #2 on: November 24, 2008, 07:56:17 AM »
Yes, Planning for the Exceptions can save lot of headache. ( You know, I/O related exceptions are my best friends  :D )

Armando

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 2,727
    • View Profile
    • Donate to Member
Re: Programming strategies -- dealing with exceptions (... in Java)
« Reply #3 on: November 24, 2008, 09:51:31 PM »
i believe that one should be planning what exceptions can be thrown and how to deal with them as you are doing class design.

OK, thanks. That's where I tended to lean towards to.

I still wonder how to include "exceptions planing" in my algorithm. Any simple guidelines or references ?


it's not something that should be left for later -- there are very important decisions (which are hotly debated in the programming world) about whether you are going to use exceptions only for catastrophic errors, or for relatively-normal function returns called with out of range parameters, etc.

Thanks. Seemed to me that it might be preferable to deal with simple out of range parameters, etc. with test/validation lines (conditions, etc.) and then use exception handling for relatively "catastrophic" errors. But then, I guess it depends on the situation (I'm thinking of errors that could become Null Pointer Exceptions if not dealt with properly at the right moment, etc. -- but I might be wrong.)

Then I guess I have to decide whether it's better to manage my exceptions at the sub-class/sub-method level (not sure hoe to qualify that...) or at the super-class and "main" level (propagating exceptions using "throws" and "throw")...


what's your own position (mouser, or mahesh2k... or whoever who'd like to share...) ? Maybe is that a dumb question, impossible to really give an answer too...  :-[
« Last Edit: November 24, 2008, 09:53:47 PM by Armando »

megatron

  • Participant
  • Joined in 2008
  • *
  • default avatar
  • Posts: 23
    • View Profile
    • Donate to Member
Re: Programming strategies -- dealing with exceptions (... in Java)
« Reply #4 on: January 09, 2009, 12:25:02 PM »
One thing I want to tell about exceptions (I am not sure if it is relevant in this thread  :D)
1.   you don’t know why it is thrown
2.   how to handle it

if you handle a exception without knowing the root cause, it may hide the internal problem which should be fixed properly at the place where it was originated.