Is stubbing out a catch block ever a valid thing to do?
Code :
JTextArea textArea = new JTextArea();
highlightAll.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
textArea.selectAll();
Highlighter highlighter = textArea.getHighlighter();
highlighter.removeAllHighlights();
try {
highlighter.addHighlight(0, textArea.getText().length(), DefaultHighlighter.DefaultPainter);
} catch (BadLocationException exception) {
// BadLocationException stubbed out - exception needs to be handled but can't occur here.
}
}
});
In the above code the JVM forces me to handle the BadLocationException that might be thrown by the call to the DefaultHighlighter. But, as I'm highlighting all text in the JTextArea, I pass in a start location of 0 and a stop location of the length of whatever text happens to be in the text area. As such, to my eyes, this code would never result in a BadLocation being passed and therefore would never result in a BadLocationException.
A couple of questions then:
Have I even understood the purpose of the possible cause of the BadLocatonExample exception correctly or is my assumption about bad start and stop locations incorrect to start with?
If not am I also correct in that, due to the nature of what I'm attempting in the code, the exception could never be thrown?
And lastly, if I am right about there being no possibility of the exception being thrown is it therefore valid to stub out the catch block as per my inline comment in the code?
Re: Is stubbing out a catch block ever a valid thing to do?
If the Exception will never be thrown, what's the harm in putting in a simple error message or something? There are four options:
The error never occurs and you don't do anything about it: everything is ok
The error never occurs and you DO do something about it: everything is ok
The error occurs and you don't do anything about it: this is bad
The error occurs and you DO do something about it: everything is ok
The point is, the only "bad" scenario is if you don't have anything in that block. What are you saving by keeping it empty? And I assure you that cases that you think will never throw an error invariable eventually do. You'll save yourself a lot of headaches by getting into the habit of never having an empty catch block.
Re: Is stubbing out a catch block ever a valid thing to do?
Code :
catch (BadLocationException exception) {
JOptionPane.showMessageDialog(null, "o_O an error that can't happen just happened", "Impossible Error", JOptionPane.ERROR_MESSAGE);
}
Adding code like the above for the sake of adding something surely doesn't really achieve anything but I take your point, the good habits are the ones worth forming when you're starting out.
In fact I had added code just like the above (only with a slightly more relevant error message) originally but ended up with a niggling feeling of having added something pointless - all those many hours trying to write the tightest possible assembly language code has left me averse to adding anything I deem unnecessary I suppose. :)
Re: Is stubbing out a catch block ever a valid thing to do?
Why not just a simple exception.printStackTrace()?
Re: Is stubbing out a catch block ever a valid thing to do?
Yeah, that makes much more sense to me - appreciated. :)