ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Other Software > Developer's Corner

Programming strategies -- dealing with exceptions (... in Java)

(1/1)

Armando:
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.



mouser:
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:
Yes, Planning for the Exceptions can save lot of headache. ( You know, I/O related exceptions are my best friends  :D )

Armando:
i believe that one should be planning what exceptions can be thrown and how to deal with them as you are doing class design.
-mouser (November 24, 2008, 01:05 AM)
--- End quote ---

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.
-mouser (November 24, 2008, 01:05 AM)
--- End quote ---

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...  :-[

megatron:
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.

Navigation

[0] Message Index

Go to full version