I do not know the context of this code, but it does not call for a custom exception. Just use a loop to display the dialog if the user enters an invalid input:
Pattern p = Pattern.compile("^[0-9]{0,3}$");
Matcher m = p.matcher(in);
while (!m.find()) {
in = JOptionPane.showInputDialog(null, "Please only enter four integers:");
m = p.matcher(in);
}
// ...
Also, you want to change if(m.find())
to if(!m.find())
otherwise the "Please only enter four integers:" dialog will only show when the user enters the correct number of integers.
If you must use exceptions, just create a class that extends the Exception
class:
public class MyException extends Exception {
public MyException(int amount) {
super("Only " + amount + " integers are allowed");
}
}
And implement it in your if statement:
if (!m.find()) {
throw new MyException(4);
}