Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Aaron Coursolle
Aaron Coursolle
18,014 Points

Okay, I'm looking at people's answers and wondering how everyone knows...?

In many of the examples I see something like this in the Main.java:

Prompter prompter = new Prompter();
        String story = null;

//This is the block where I'm confused:
 try {
            story = prompter.promptForStory();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

//code continues
Template tmpl = new Template(story);
        prompter.run(tmpl);
    }

I understand the big picture concept of trying and catching, but what is the clue that we need one here?

1 Answer

Simon Coates
Simon Coates
28,694 Points

I'm not familiar with the specific java code you're asking about but there are two categories of exception (checked and unchecked). One group means you messed up ("Unchecked runtime exceptions represent conditions that, generally speaking, reflect errors in your program's logic and cannot be reasonably recovered from at run time."), while others are a result of your code needing to deal with things that have an innate danger of crashing (for example running out of disk space). If you look through the java documentation, a lot of the methods list the potential exceptions they can cause. A good programmer avoid avoidable exceptions (should avoid a try catch to deal with unchecked exceptions) and uses try and catch when exposing themselves to something where crashing may be unavoidable. So you should deal with any potential for checked exceptions, or mark your code with a throws statement (forces the calling code to deal with the potential exception in order to compile). (i hope i got that right - it's been 7 years since i did java 101)