The empty catch is fine. They are not usually empty, but it is fine for them to be so. It is not uncommon for exceptions to make better control flow than if statements. In fact, that was the reason exceptions were introduced in the first place, to collect your error handling into a better location. Before exceptions, it could get quite messy, especially if you had to jump stack frames.
Take the following example:
public void mainLoop()
{
while(true)
{
try
{
Thread.sleep(1000);
f();
}
catch (InterruptedException ex)
{
// Don't care; this thread expected to not die when interrupted
}
}
}
Sometimes, that really is the best way to handle it.
And as for the "exceptions should not be used for control flow" argument...
Also, sometimes it is convenient to collect all of what you consider your error cases to be into one place, and if they all share the same logic then you can even use a multi-catch.
public void verifyInput()
{
try
{
int n = Integer.parseInt( txtInput.getText() );
if(n < minimum || n > maximum)
throw new MyDataInvalidException("data out of bounds");
sendFormData();
}
catch (NumberFormatException | MyDataInvalidException ex)
{
// Don't care. Each widget on the form is already set up to display
// information about invalid input, so if you press "enter" in
// this state just ignore it
}
}
Now, the above code sample has the added benefit that if, later on, you get a new requirement to handle bad input differently, you can maintain this quickly and easily.
For example, you get complaints that each GUI widget showing its invalid state is not enough, "Please, if I press enter show me a dialog with 'Invalid data: please check all fields.' to let me know I need to scroll up to see the text field's "X blah blah error message"" Now you just replace that comment with a message box popup, perhaps.
Or, if it needs to be handled higher up the stack, change verifyInput
to use throws.