Exception handling or return value
-
Hi there, Please specify how to decide whether to go for return codes or exception handling. When is exception handling preferred over returning a specific error code. Thanks in advance, Raja Pratap
-
Hi there, Please specify how to decide whether to go for return codes or exception handling. When is exception handling preferred over returning a specific error code. Thanks in advance, Raja Pratap
IMHO you need a decision between throwing an exception or returning a specific error code. If it is as above stated, then I think: (1) If it's your own application (entirely developed by you) then you can (almost) safely rely on return error codes, since you know that calling methods must always check the return values. (2) If your're working in team then throw, throw, throw: never trust your mates coding neatness!!! Hope that helps. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
IMHO you need a decision between throwing an exception or returning a specific error code. If it is as above stated, then I think: (1) If it's your own application (entirely developed by you) then you can (almost) safely rely on return error codes, since you know that calling methods must always check the return values. (2) If your're working in team then throw, throw, throw: never trust your mates coding neatness!!! Hope that helps. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
Thank you for your response Pillani. But still I'm not clear, why return codes are not suitable when we work as team.
-
Thank you for your response Pillani. But still I'm not clear, why return codes are not suitable when we work as team.
Hi, I think it is because you can not be totally sure about what the others will do. If they get an assertion, they have to solve something, if they have a return code, they can make nothing about or even don't realize about the error. And then error will be cumulating themselves until it would be too difficult to correct the program.
Greetings. -------- M.D.V. If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
-
Thank you for your response Pillani. But still I'm not clear, why return codes are not suitable when we work as team.
Raj Prathap wrote:
But still I'm not clear, why return codes are not suitable when we work as team.
Well, suppose you have a method returning failure. If the caller doesn't check the return value and continues execution, maybe some disaster is along the way. On the other hand, throwing an exception, you force the caller to handle it or, anyway, the execution will be stopped. Now, if the caller is a member of you team (and not yourself) then there is a chance that importance of return value is underestimated. Hope that helps :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
Raj Prathap wrote:
But still I'm not clear, why return codes are not suitable when we work as team.
Well, suppose you have a method returning failure. If the caller doesn't check the return value and continues execution, maybe some disaster is along the way. On the other hand, throwing an exception, you force the caller to handle it or, anyway, the execution will be stopped. Now, if the caller is a member of you team (and not yourself) then there is a chance that importance of return value is underestimated. Hope that helps :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
Thanks Pallini, That answers my question.