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 - Retired Organizing Data Serialization

Thomas Williams
Thomas Williams
9,447 Points

catching an IOException that is thrown automatically

In this lesson where an ObjectOutputStream is used to save an Array using WriteObject a catch block is added to handle an Exception eg ("Problem with saving") but the throw Exception was not written by us in the code, therefore how are we supposed to be aware that we need to catch it? I only know we needed a catch block because craig told me!

1 Answer

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hey there Thomas,

A lot of methods on classes that we use are going to have to be looked up like Craig did, if you remember Craig once didn't catch an exception and his program failed to compile to telling him that there was an uncaught exception.

He did this exactly to get us ready to look at the object just like he did, looking up the ObjectOutputSteam class' java doc We can see that the writeObject method throws multiple methods, and Craig went through and caught and looked at what exceptions it threw, in this objects case the java doc tells us it is possible to get an IOException, so Craig prepared his program to catch it.

Thanks, I hope this helps.

Thomas Williams
Thomas Williams
9,447 Points

Ok, so it is just a case of looking up each method, I thought maybe it was predictable based on the function of the method. Thank you for clearing that up Rob!