Forums Register Login

Throwing exceptions

+Pie Number of slices to send: Send
Hello,

If I have a controller which calls other classes for processing e.g. reading data from db and if I want to throw a custom exception for a particular function in one of the helper classes, then should I throw the exception in the helper class itself or in the controller class using setting up flags in helper classes?
If I throw the exceptions only in a single class, then its easy to maintain whereas if I use helper class, no need to setup flags for each and every case.
What about "Throw early catch late" principle here.

Please advise.
+Pie Number of slices to send: Send
Hi pramod,

It is not a good practice to set up flags at low level methods, and then at high level methods, throw exception based on those flags.

The ideal way is to catch a real-life exception, and then throw custom exception based on it. e.g.



The important thing to take care in this approach is - you need to be careful about which part you want to execute after (or even after) the exception (e.g. cleaning up the resources etc.) - because once an exception is thrown, it will keep on propagating till it finds a handler.

I hope this helps.
Do Re Mi Fa So La Tiny Ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 658 times.
Similar Threads
how to convert-checked exception to checked exception and to unchecked exception
cast in class hierachy
JSF 2.0 exception handling - Is it necessary to catch the exceptions in the Bean classes ?
exam lab practice exam -1, question24 need justification?
Design issue in presentation tier
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:02:23.