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?