I am having a function that does a DB operation and sends a boolean value for success/failure. suppose it fails, i want to send the failure reason as a return value.
for now, i have defined return value as a string. if it is failure i return "failure: reason" and if it is success, i return "success". but this is not good practice.
This is what you use
throw exceptionfor. You can very well change your method signature and set the return value to be boolean. Then create a custom Exception class:After that, whenever you find a reason for failure, do this:
In your controller (or service), catch the exception: