• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Implementing Serializable

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code is my "java practice" versions of the Dog and Collar classes taken from the book "SCJP Sun Certified Programmer for Java 6 Study Guide (Exam 310-065)" Chapter 6 Certification Objectives—Serialization (Exam Objective 3.3).

I expected the code to produce a runtime exception because the Collar class did not implement Serializable, but the code compiles and runs producing the following output.

Collar@578ceb
5
yes

Question 1. Why didn't I get an exception?
Question 2. What exactly does the output Collar@578ceb represent?






 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Edward Lawrence wrote:
Question 1. Why didn't I get an exception?



Why do you think you didn't? How would you know if you did?

Question 2. What exactly does the output Collar@578ceb represent?



When you print out an object, the println() method ends up calling that object's toString() method. If you don't override toString(), you get Object's version. That's what you're seeing.
 
Edward Lawrence
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I didn't get an exception because I got the output 5. However, I realized that I did not handle any possible caught exception in my catch block. I probably wrongly deduced that if 5 printed I did not get an exception. I will try it with printing any possible caught exception.

If I get an exception printed my question would be why did it produce the expected result?
 
Edward Lawrence
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. I did get the following exception with the expected results.

Question: Why did it go ahead and produce the correct results after the exception?

java.io.NotSerializableException: Collar
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException
: Collar
Collar@578ceb
5
yes
 
Sheriff
Posts: 26962
84
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Edward Lawrence wrote:Question: Why did it go ahead and produce the correct results after the exception?



Because that's how your code is written. You catch the exception and handle it (by writing it to System.out, I suppose) in lines 39-43 and then continue to the next statements in lines 44-46, which do what you call "producing the correct results".
 
Edward Lawrence
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Paul. I am relatively new to Java and I am leaning by writing exprimental code. I am closing this question.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Edward Lawrence wrote:Yes. I did get the following exception with the expected results.

Question: Why did it go ahead and produce the correct results after the exception?



It didn't.

The collar variable never got set to its new value because of the exception. Then you carried on after the exception as if everything was fine (even though it wasn't--the serialization/deserialization failed). Then you printed out some collar info, which still had its original value. It just so happens that collar having the original value looks exactly the same as collar after the results of a successful deserialization.

In other words, the particular incorrect results you had are indistinguishable from the desired results.

When an exception occurs, you need to either actually handle it (meaning you retry or use some default value or do something that can properly serve as "success" in that context), or else you don't catch it in the first place, or if you do, you log, wrap, and rethrow.

Merely catching an exception doesn't fix the problem.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic