Exceptions/return types, correct way to handle the flow in your app?
I have a class which acts as my model. I have a class that handles all the database calls. And of course my gui classes.
We can take one of the actions as an example, create directory.
The gui calls the model with appropriate data to create a directory, lets say only its name. The model passes this info to the database handler.
Which of the following is a better option?
void/boolean return type in the model磗 createDir method?
void/boolean return type in the dBase handler磗 createDir method?
Which(/none/both) of the two methods should throw some kind of DirectoryNotCreateableException
As often, there might be no 100% right/wrong but I just want to check if there is some obvious way or a more preferrable way.
if you use exceptions it will easily allow you to seperate the flow of the error handling from flow of the application. Also exceptions allow you to transfer more information about the error as message.
Normally When I am developing the application.
Whenever an error condition is met I throw a Application Level exception with a proper message.
And the catch blocks that catch them invoke a common function (Normally I call it ErorSYS.handleException(Throwable t)) which will do commn stuff like showing the stack trace and giving the user an error popup message. And roll back the transaction if needed.
Additional specific tasks can be done in the catch block where you catch the exception.
Using boolean returns will make things little messy
LRMKa at 2007-7-14 21:11:01 >

Thankyou for your thoughts.
So what concerns my app, your recommendation would be that if something磗 going wrong in the database handler, it throws an exception, which the main model class can propagate on to the calling gui class (or whoever the caller is), which then can show some sensible message to the user or take the appropriate actions.
That sounds good to me. Right now I don磘 know how I have succeded to mix exceptions with booleans in some not so healthy way.
>which then can show some sensible message
Remember its not the GUI or the caller give the message
Message should be set ar the message of the exception by who ever throws the exception. Becouse that is the block of code which knows best about the problem.
Ex:-
if (orderQty <= 0)
throw new ApplicationException("The Order Quantity should be greater than zero.");
And in the function that process the exception will look like this
public void processException(Throwable t){
if (t instanceof ApplicationException)
JOptionPane.showMessageDialog(null,t.getMessage());
else if (t instanceof InvocationTargetException)
processException(t.getCause());
else{
t.printStackTrace();
JOptionPane.showMessageDialog(null,"Unexpected Exception! Contact Admin");
//You might want to log this one including the stack trace for later use
}
}
LRMKa at 2007-7-14 21:11:01 >
