View Single Post
  #2   Spotlight this post!  
Unread 12-02-2012, 22:42
Patrick Chiang Patrick Chiang is offline
Programming
FRC #3070 (Team Pronto)
Team Role: Mentor
 
Join Date: Feb 2009
Rookie Year: 2009
Location: Seattle
Posts: 162
Patrick Chiang is a name known to allPatrick Chiang is a name known to allPatrick Chiang is a name known to allPatrick Chiang is a name known to allPatrick Chiang is a name known to allPatrick Chiang is a name known to all
Re: Exception Handling Best Practice

Use try-catch. But most importantly, keep them separate, and put them at the end of any piece of code that only runs once.

That way, if the camera suddenly gets unplugged during a competition, your robot won't just sit there and wait until it gets image.

Here's a (very real) example of what might cause problems:
Code:
public void robotInit(){
    try {
        // camera init code
        // joystick init code
        // jaguar init code
    } catch (Exception ex){
        ex.printStackTrace();
    }
}
If your camera code is broken, the joystick and jaguar init code will not execute, and your robot will not run. The solution is to take the joystick and jaguar init code out of the try-catch.

Try-catch blocks aren't a cure for cancer, or robot crash. What you want to do with try catch is to make sure that you're anticipating possible errors, like camera taking time to boot up before it's ready to get image, components breaking in the middle of a competition, wires becoming unplugged.

You want to make sure that if something is broken, everything else (that doesn't depend on it) still works. THAT, I believe, is the point of catching exceptions.

Some people might take issue with me try-catching using a general exception, but I disagree and would actually recommend that for competition. You want to make sure nothing silly escapes your try-catch and crash your robot.
Reply With Quote