• 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

My exception programm

 
Rancher
Posts: 163
5
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello! While learning for OCAJ with book from Edward Finegan I did exercise 9-2 for Chapter Handling Exceptions
and I am so happy that I wrote myself a program which handle exception that I want publish it here for reviewing or questions:




 
Mike Savvy
Rancher
Posts: 163
5
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just opened other topic about exceptions (https://coderanch.com/t/678275/java/Exception-handling) and see that I forget to use "super" for my exception constructors.
Why I need it?
And why when I use super(s) instead of print function, the string will be not printed?
 
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, it's because MyException is never executed.  It would only be executed if today.codeToday == false.

Also, don't write if (today.codeTaday == false), write if (! today.codeToday).
 
Mike Savvy
Rancher
Posts: 163
5
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:Well, it's because MyException is never executed.  It would only be executed if today.codeToday == false.

Also, don't write if (today.codeTaday == false), write if (! today.codeToday).



"== false" and "!" have exactly the same result. The problem is that String "Hi I am exception" is not printed though it is exception and I see red text "Omg and so on...".
So I do not understand what does "super" mean here:
 
Knute Snortum
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So I do not understand what does "super" mean here:


It calls the constructor of the super class, which in this situation is Exception.

"== false" and "!" have exactly the same result.  


That's true, but the latter is better code than the former.

The problem is that String "Hi I am exception" is not printed though it is exception and I see red text "Omg and so on...".


That's because you're catching the exception when you throw if in a try/catch block.  Try this:


 
Marshal
Posts: 27450
88
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

Mike Savvy wrote:"== false" and "!" have exactly the same result.



Yeah, they do. But one day you're accidentally going to write "= false", which will compile just fine but it's not going to give you the same result all the time. And it's a devil of a bug to find. So take the advice and use "!" instead.
 
Marshal
Posts: 76825
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why have you declared your constructors with throws MyException?
The field which Eclipse suggested you add (it was a suggestion, not an instruction), the SUID as I call it, is a hash of the class. If you serialise an object and its SUID is different from that in the current version of its class, the deserialisation process will fail with an exception. You have several options:-
  • 1: Leave the SUID out, which means the deserialisation will proceed regardless of any changes.
  • 2: Calculate an SUID with the serialver tool (or the option in Eclipse). Deserialisation will proceed if the SUIDs are the same, otherwise you will suffer an exception.
  • 3: Copy the SUID from an earlier version of the class. That asserts to the deserialisation mechanism that it is using the same class version and deserialisation will proceed normally.
  • 4: Guess an SUID (e.g. 1L). All versions of the class with the same SUID will deserialise normally.
  • I think that fields added in later versions (after serialisation) will be left with their default values after deserialisation. I think.

    Your DaysOfCode class shows inconsistent indentation. That is likely to trip you up.
     
    Mike Savvy
    Rancher
    Posts: 163
    5
    Eclipse IDE Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you for explanations guys !
    Actually I understand better
     
    Campbell Ritchie
    Marshal
    Posts: 76825
    366
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    On behalf of everybody, “That's a pleasure.”
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic