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 Java Data Structures Efficiency! Design the UI

throws IOException after the method signature

Hey Craig,

I would like to get a more detailed explaination what you did as you added that "throws IOException" after the method signature of promptAction().

I only know throwing exceptions explicitly by "throw new Exception()" or the automatically way. Since I come from C#.NET I actually never saw that way with the method signature.

Does it maybe mean that whatever Exception is gonna occur in this method it will always unwind with an IOException back to the method caller/client ?

Thanks for your help in advance

2 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

It means that the future caller of the method will need to handle it. If you don't put that in the definition it means you need to handle it. It won't compile without it. Try removing it and check the compilation error. Basically imagine it as passing the buck because you aren't exactly sure how the caller of your method would want the problem to be handled. Here's a pretty good explanation of the practice.

The only types of exceptions that don't require this logic are Runtime (or unchecked) Exceptions. And that too is debatable.

So Java forces the programmer to handle the checked (possible exception) by himself or throw it to the caller... I can't try it out by myself right now but - how does the programmer know which checked exceptions might occur within his method/class so he has to throw it/handle it by himself?

does the compiler tell him that there's an exception which he should take care of?

As I said I'm from .NET and there is no such thingie (or I never came across it).

Craig Dennis
Craig Dennis
Treehouse Teacher

Yes the compiler will warn you and most IDEs use it to let you know and do suggest the wrapping or throwing for you. Once you get used to it, it is pretty nice, but seems overwhelming at first.

I also looked it up, and each method has a list of exceptions it automatically throws at the bottom of the java documentation at the oracle website. Ex: See the bottom of the long documentation for the readLine method for BufferedReader: http://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html