This is my program structure:
main function -> call Class A obj -> Class A calls Class B obj
when it comes to exception handling, I want to exit the program with error code if exception is encountered either in main, class A or class B.
Currently, I am doing what I feel is a cumbersome process (and likely a foolish way):
-> Have exception handling within the functions of Class A and Class B.
-> Have the function exit with a value deemed 'error' (such as specific value or boolean false) if exception occurred in function.
-> main has its own try/catch but also for each function call of class A and class B, if it return is 'error', it throws exception and then exits with error code.
My contention with this approach is (a) I am throwing and handling exception potentially multiple times (at the class level and then at main level). (b) If a function in class is suppose to return a value, than I have to set aside a specific value that when received, should be interpreted as error.
any cleaner suggestions? This is for programs which will be compiled to run as executable JAR from command line and won't be a micro service (or web server based -- for those, I reckon, instead of exiting the program, you would report the error in log and continue on).