aspose file tools*
The moose likes Beginning Java and the fly likes exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "exceptions" Watch "exceptions" New topic
Author

exceptions

johnathan smith
Greenhorn

Joined: Mar 30, 2007
Posts: 21
hey there guys
cheers for the help on my previous topic, was very much appreciated

i have a new problem now which any help would be very much appreciated
my class player takes information such as games played, times dismissed etc

i was wondering would i be able to throw an exception if the number of times dismissed is less than the games played??

my player class code is below
any help would be greatly apprecaited


thanks


[ April 22, 2007: Message edited by: johnathan smith ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
The short answer is, yes.
The long answer is y-e-e-s.

Show us what you are suggesting. It should be easy enough to implement. At least if you are playing cricket and not baseball . . .
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
BTW: Shouldn't batting average be worked out as a floating-point number? You will have to work out how to get runsScored / timesDismissed as a double return value.
[ April 22, 2007: Message edited by: Campbell Ritchie ]
johnathan smith
Greenhorn

Joined: Mar 30, 2007
Posts: 21
i assure you its definately cricket

what im wanting to do is as follows
when creating a Player obviously you enter the details
and if the user enters the more dismissials than the amound of games played (i.e this is impossible) then an excpetion this thrown

any idea how i can do this?
thanks
johnathan smith
Greenhorn

Joined: Mar 30, 2007
Posts: 21
...or an error message is printed!

thanks
Sidd Kulk
Ranch Hand

Joined: Feb 20, 2007
Posts: 152
You can provide a check in the Constructor itself and then throw a RuntimeException if the number of dismissals is greater than the number of matches played.

Njoy!!
Sid
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Johnathan Smith, you are quite correct about how to do it, but I would suggest you create your own Exception by inheriting from a suitable Exception class.

You have to provide four constructors which match those in Exception, and don't have to do anything with the methods.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
. . . and surely by numberOfDismissals > numberOfGames you mean


numberOfDismissals > numberOfInningsPlayed
?
 
jQuery in Action, 2nd edition
 
subject: exceptions