Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Is stubbing out a catch block ever a valid thing to do?

  1. #1
    Junior Member pmc's Avatar
    Join Date
    Nov 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Is stubbing out a catch block ever a valid thing to do?

    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?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,254
    My Mood
    Hungover
    Thanks
    142
    Thanked 617 Times in 528 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member pmc's Avatar
    Join Date
    Nov 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Is stubbing out a catch block ever a valid thing to do?

    	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.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,254
    My Mood
    Hungover
    Thanks
    142
    Thanked 617 Times in 528 Posts

    Default Re: Is stubbing out a catch block ever a valid thing to do?

    Why not just a simple exception.printStackTrace()?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member pmc's Avatar
    Join Date
    Nov 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Is stubbing out a catch block ever a valid thing to do?

    Yeah, that makes much more sense to me - appreciated.

Similar Threads

  1. InputMismatchException try catch block not working as expected
    By mikhl in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 23rd, 2011, 06:02 PM
  2. This might be the easiest thing to solve.
    By tn12llmark in forum Object Oriented Programming
    Replies: 1
    Last Post: November 2nd, 2011, 07:49 AM
  3. try-catch block reports to have an Error that I don't know how to fix
    By baraka.programmer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 24th, 2011, 06:27 AM
  4. [SOLVED] Method help, one thing then another - puzzle
    By Scotty in forum Java Theory & Questions
    Replies: 3
    Last Post: April 28th, 2011, 01:52 PM
  5. Weird thing with JFrame
    By Brt93yoda in forum AWT / Java Swing
    Replies: 2
    Last Post: August 23rd, 2010, 05:00 PM