I want to create my own exception class and catch the null value returned when the user presses the inputDialog boxes cancel button. Basically If the user presses cancel I dont want the program to crash. how do i do that. and I wanted to create my own exception class because I intend to put other custom exceptions in it for future use.
static private String showInputDialog()//utility function for userInput----------------
{
String inputValue = JOptionPane.showInputDialog("Please input something");
if(inputValue.isEmpty() || !inputValue.matches("[A-Za-z]*"))
{
inputValue = showInputDialog();
}
return inputValue;
}
//where inputDialog is called
public void actionPerformed(ActionEvent evt)
{
String firstName = showInputDialog();
String lastName = showInputDialog();
}
The exception is the result of a
nullvalue of inputValue, you can prevent getting the exception by checking for null yourself.Also, your method now goes into recursion on every next iteration. What you want to achieve functionally is “while nothing is input ask for input”. This would translate into: