Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[newbie] Throwable Exception

 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • 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
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for walking me through that, seems more elegant than my original code

 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • 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?
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • 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!
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Rob:Do you use leading underscores for both locals *and* members?
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • 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
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, okay; missed that.

At OP then--don't mix where you use underscores! It's confusing :)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic