File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Write My Own Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Write My Own Exception" Watch "Write My Own Exception" New topic

Write My Own Exception

Scott Purcell

Joined: Dec 13, 2001
Posts: 1
I failed a question on my final exam last night because I did not know how to create and use my own exception. The mission was to create a class that extends exception, then have another class use that exception. The basic problem I had to deal with was add 2 numbers, if they were greater than 100 use my exception to explain that the two numbers were greater than 100, else print that all is good. It was a simple console app, and I have been fighting it all morning and still cannot do it.
I have the following 2 classes
package finalexam;


Thanks for any assistance
[This message has been edited by Cindy Glass (edited December 14, 2001).]
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
You have your throw new toohigh in the wrong place.
Your check method should instead look something like this:

And take that throw statement from your catch block. Not sure why you were trying to return an int from this method if all you want to do is check to see if it is above 100 or not, and if it is, throw an exception. If anything, you MIGHT return a boolean, but I don't see that being needed.

[This message has been edited by jason adam (edited December 13, 2001).]
I agree. Here's the link:
subject: Write My Own Exception
It's not a secret anymore!