aspose file tools*
The moose likes Beginning Java and the fly likes [newbie] Throwable Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "[newbie] Throwable Exception" Watch "[newbie] Throwable Exception" New topic
Author

[newbie] Throwable Exception

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

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


Jon
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
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

Joined: Oct 27, 2005
Posts: 19720
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

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

Joined: Oct 27, 2005
Posts: 19720
    
  20

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

Joined: Apr 25, 2008
Posts: 660

Thanks for walking me through that, seems more elegant than my original code

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

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

Joined: Oct 13, 2005
Posts: 39409
    
  28
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

Joined: Sep 29, 2008
Posts: 12617

@Rob:Do you use leading underscores for both locals *and* members?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
David Newton wrote:Do you use leading underscores for both locals *and* members?
You shouldn't use underscores for either.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Oct 27, 2005
Posts: 19720
    
  20

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

Joined: Sep 29, 2008
Posts: 12617

Oh, okay; missed that.

At OP then--don't mix where you use underscores! It's confusing :)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [newbie] Throwable Exception