DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

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

Armando

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 2,727
    • Coding Snacks
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.



"I suppose it can be said that I'm an absent-minded driver. It's true that I've driven through a number of red lights on occasion, but on the other hand, I've stopped at a lot of green ones but never gotten credit for it."
Glenn Gould
« Last Edit: November 24, 2008, 04:59 AM by Armando »

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,931
    • Gathering Badge
    • Plugin Coding Medal
    • Participated in NANY Challenge Seventeen Times
    • Cody Award
    • Coding Snacks
    • Volunteers/Staff
Re: Programming strategies -- dealing with exceptions (... in Java)
« Reply #1 on: November 24, 2008, 07:05 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,426
    • Participated in NANY Challenge Twice
Re: Programming strategies -- dealing with exceptions (... in Java)
« Reply #2 on: November 24, 2008, 01:56 PM »
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
    • Coding Snacks
Re: Programming strategies -- dealing with exceptions (... in Java)
« Reply #3 on: November 25, 2008, 03:51 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.

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...  :-[
"I suppose it can be said that I'm an absent-minded driver. It's true that I've driven through a number of red lights on occasion, but on the other hand, I've stopped at a lot of green ones but never gotten credit for it."
Glenn Gould
« Last Edit: November 25, 2008, 03:53 AM by Armando »

megatron

  • Participant
  • Joined in 2008
  • *
  • Posts: 23
Re: Programming strategies -- dealing with exceptions (... in Java)
« Reply #4 on: January 09, 2009, 06:25 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.

Original address: https://www.donationcoder.com/forum/index.php?topic=15861.0