• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

[newbie] Throwable Exception

 
Ranch Hand
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I created a new exception and would like to know whether it is fine...







1. Is there some reference where I can lookup existing exceptions? The API s***s sometimes
2. Specifically does anyone know of an existing exception that caters for 'Invalid Cast'?


NOTE: It would be helpful if I could update .zip and .rar files to this forum, or that a warning is displayed before reading
'Allowed file types'. Unless one is already available
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jon:

You shouldn't be subclassing Throwable directly. The class you're probably looking to extend is RuntimeException. You can look here or here for some documentation.

John.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's wrong with plain old java.lang.ClassCastException? It will be thrown automatically if a cast fails.

Jon Camilleri wrote:1. Is there some reference where I can lookup existing exceptions? The API s***s sometimes


At the top, click Tree. Look for java.lang.Throwable. Check out the reeeeaaaaally big list of exceptions available
Also, the most used exceptions are in packages java.lang, java.io and java.util. Open those package and scroll to the bottom. There are separate sections for exceptions and errors.

2. Specifically does anyone know of an existing exception that caters for 'Invalid Cast'?


See my first line.
 
Jon Camilleri
Ranch Hand
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:What's wrong with plain old java.lang.ClassCastException? It will be thrown automatically if a cast fails.

Jon Camilleri wrote:1. Is there some reference where I can lookup existing exceptions? The API s***s sometimes


At the top, click Tree. Look for java.lang.Throwable. Check out the reeeeaaaaally big list of exceptions available
Also, the most used exceptions are in packages java.lang, java.io and java.util. Open those package and scroll to the bottom. There are separate sections for exceptions and errors.

2. Specifically does anyone know of an existing exception that caters for 'Invalid Cast'?


See my first line.



Thanks.

1. Well now I'm getting the following error:

Unresolved compilation problems:
Syntax error, insert "enum Identifier" to complete EnumHeaderName //I've tried to resolve this error by commenting out @Suppresswarnings
Syntax error, insert "EnumBody" to complete BlockStatement

Do you think it is related to the equals method below? (that's the last change so I assumed I've done a mistake somewhere)

2. Another problem is that I need to return false when the cast fails, and, this conflicts with the throw exception (unreachable code). To go about it I removed the Exception from the .equals method.

Is it possible to have the best of both worlds:

1. return false?
2. throw an invalid cast exception?

My gut feeling tells me it is not good design to have both, am I correct or just being lazy?


3. When using the @Override before the method declaration, I am getting an error:


Unresolved compilation problems:
The method Name() is undefined for the type Object
The method Salary() is undefined for the type Object
The method HireDay() is undefined for the type Object

My concern is that the author of the book I am reading(1) says that it is a common mistake not to have the override, however, I see no other choice than to have the Employee as the explicit parameter. Any other ideas?





Reference book
1. Core Java Vol 1 Fundamentals 8th Edition (pg.196) ISBN: 978-0-13-235476-9
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, the proper signature is "public boolean equals(Object anotherObject). You have created an overloaded method, that won't be called by most API calls.

Anyway, let's run through the method:

And now how I would have written it:
 
Jon Camilleri
Ranch Hand
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for walking me through that, seems more elegant than my original code

 
Jon Camilleri
Ranch Hand
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:
At the top, click Tree. Look for java.lang.Throwable. Check out the reeeeaaaaally big list of exceptions available
Also, the most used exceptions are in packages java.lang, java.io and java.util. Open those package and scroll to the bottom. There are separate sections for exceptions and errors.

What long list?

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here. Click on tree at the top, or click on java.lang, then tree at the top, depending how many thousand lines you wish to look at!
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Rob:Do you use leading underscores for both locals *and* members?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:Do you use leading underscores for both locals *and* members?

You shouldn't use underscores for either.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a religious war I'm not interested in (and I see no sense in implying any sense of obligation as to which is "correct").

I was just confused by the apparent usage for both.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:@Rob:Do you use leading underscores for both locals *and* members?


I never use underscores, I just copied them from Jon's original code.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, okay; missed that.

At OP then--don't mix where you use underscores! It's confusing :)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic