Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Data Structures Organizing Data Serialization

Thomas Williams
Thomas Williams
9,090 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,459 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,090 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!