Hi guys, I have a question about error handling. Still working on the addition application and now that I've learned what are the correct exceptions to handle I'm not sure what to do when I've established that the my inputs are all valid and print my result. Let's have a look at the calculator class again:
package my.vaadin.project.exceptionTest;
import java.awt.Component;
import java.util.InputMismatchException;
import com.vaadin.server.Page;
import com.vaadin.shared.Position;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Notification;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.event.Action;
public class Calculation extends CustomComponent{
final VerticalLayout vl = new VerticalLayout();
final TextField dividend = new TextField();
final TextField divisor = new TextField();
final TextField result = new TextField();
final Button resetButton = new Button("Reset");
private int divisionResult = 0;
Button button = new Button("Click Me");
public Calculation(){
dividend.setCaption("Enter the dividend:");
divisor.setCaption("Enter the divisor:");
result.setCaption("Result");
result.setReadOnly(true);
button.addClickListener( new Button.ClickListener(){
@Override
public void buttonClick(ClickEvent event) {
System.out.println("this is a test");
validateInputs();
}
});
resetButton.addClickListener(new Button.ClickListener(){
@Override
public void buttonClick(ClickEvent event) {
setDividend("");
setDivisor("");
setResult("");
}
});
vl.setMargin(true);
vl.setSpacing(true);
vl.addComponents(dividend, divisor, button, result );
}//end of constructor
public void validateInputs(){
System.out.println("theDivisor is " + getDivisor() + " theDividend " + getDividend());
try{
divisionResult = ( Integer.parseInt(getDividend()) / Integer.parseInt(getDivisor()));
}
catch(ArithmeticException arithmeticException){
System.err.println("Zero is an invalid denominator!");
createError("Divisor can't be 0!! Please enter a number > 0!");
}
/*catch(InputMismatchException inputMismatchException){
System.err.println("The dividend or divisor are not a number! Please enter a valid number!");
createError("The dividend or divisor are not a number! Please enter a valid number!");
}*/
catch(NumberFormatException numberFormatException){
System.err.println("The dividend or divisor are not a number! Please enter a valid number!");
createError("The dividend or divisor are not a number! Please enter a valid number!");
}
System.out.println("after the catches");
}//end of validateInputs
public String getDivisor(){
return divisor.getValue();
}
public String getDividend(){
return dividend.getValue();
}
public void setDivisor(String newDivisor){
divisor.setValue(newDivisor);
}
public void setDividend(String newDividend){
dividend.setValue(newDividend);
}
public void setResult(String newResult){
result.setValue(newResult);
}
public void createError(String errorString){
String error = errorString;
Notification notif = new Notification(
error,
Notification.TYPE_ERROR_MESSAGE
);
notif.setDelayMsec(20000);
notif.show(Page.getCurrent());
}
}
So, I have a try/catch statement sitting inside the validateInputs()
method which gets called when the button is clicked. Now, let's assume that my inputs are both valid, I need to display the result in my result
text field.
So, where would I put the code that displays the result? Whether or not an exception is thrown, control is then returned to the first line after tha catch statement as far as I know, and if there isn't any, back to where the function containing the try catch block is: I need a way to determine whether an exception has been thrown or not and if it has, do nothing other than displaying the error (as I'm doing currently) and if it hasn't, displaying the result. So, what I thought I'd do, is that I could get my validateInputs()
to return a boolean value of false if an exception has occurred and true if it hasn't, pass that back to the caller and take it from there. I can't think of any other way. Hope that makes sense.