bluetech
April 24th, 2003, 07:19 AM
I am returning a value object from a catch block. I basically want to return error codes in a value object when my program generates an exception. Thats why i am using a return statement in the catch block.
Is this the right way to code ?
Can u suggest any alternative !!
Ishaibin
April 24th, 2003, 09:49 AM
returning the Exception e object that the catch block generates
is the standard way to debug and find errors :)
dlorde
April 24th, 2003, 12:06 PM
As Ishaibin says, exceptions are there to 'return' whatever error information is required. Using a return statement to return error or failure status defeats the whole point of using exceptions.
Throw your error value object out of your method and catch it in the calling method. The intention is that return statements can always be assumed to return valid data. Catch statements will explicitly handle errors and provide access to any error information provided. This allows you to code the body of your methods without worrying about errors and failures - leave all that to the catch statement(s) at the end. It's simpler, quicker, clearer, and easier to read and maintain.
You can throw and catch any object you like as long as it extends Throwable, but usually people will subclass Exception to make life easier. You can add whatever data members, constructors, and accessor methods you need to set and get the error data you require.
Incidentally, if there are various codes to represent different failures or errors, and they should be handled differently, you should make them separate exceptions. If you make a simple exception hierarchy, you can catch the individual exceptions where you need to, and by catching the superclasses, you can catch some or all of them in a single catch statement where you don't need to handle them separately. Total flexibility...
Why do we never have time to do it right, but always have time to do it over?
Anon
bluetech
April 25th, 2003, 12:44 AM
Thanks guys,
feeling much better to code that way. I think, m gonna take this right ahead.