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

Writing Your Own Exceptions

Michael Hubele
Ranch Hand

Joined: Dec 17, 2005
Posts: 182
I am trying to write my own exception but I am not sure if I am doing it right.

I found a tutorial and basicly it told me to what I have below but I am not sure if there is more too it or what I also am not sure how I can then get this exception to work in other java files I made.

I got like 6 files and there in a package that I called data so do I stick my exception file in the package folder and then go to every file that is is needed and type data.CodeLengthOutOfBoundsException in it?


Tutorial site: http://www.tech-recipes.com/java_programming_tips1199.html



Like to me I am sure there more to it then just that but I don't know what and I don't know what I have written would get me since I have not made a test class for it yet since I don't know yet how to get it into that test class file I am sure I need to import it or do something like that.
Mark Van Tuyl
Ranch Hand

Joined: Mar 22, 2002
Posts: 60
What you've got will indeed define a new type of Exception.

I would suggest, however, that you include the call to super() in both constructors. Otherwise, when creating an instance of the exception using the default constructor, no specific information about the exception will be available.

You might do something like this:


<a href="http://www.catb.org/~esr/faqs/smart-questions.html" target="_blank" rel="nofollow">How To Ask Smart Questions</a>
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

I agree with Mark to a point; however, the constructor that accepts a String argument should pass that String to the super() call; the other one, the one that doesn't accept a parameter, is the one that should pass the custom message to the superclass constructor. It would be very annoying to the programmer using your exception if it had a constructor that accepted, but then ignored, a parameter.


[Jess in Action][AskingGoodQuestions]
Michael Hubele
Ranch Hand

Joined: Dec 17, 2005
Posts: 182
Originally posted by Ernest Friedman-Hill:
I agree with Mark to a point; however, the constructor that accepts a String argument should pass that String to the super() call; the other one, the one that doesn't accept a parameter, is the one that should pass the custom message to the superclass constructor. It would be very annoying to the programmer using your exception if it had a constructor that accepted, but then ignored, a parameter.


I don't understand can you give me a example of what going on and how to import it into another file I still don't know how to do that.

Also is there more stuff you can add to it or is that all? like do you have to add stack traces or something like that or is it automatic like I just find that a bit too easy to write an exception.
[ April 22, 2006: Message edited by: Michael Hubele ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18887
    
    8

Yes, it is just that easy to write an Exception class That is all you have to do. (Apart from the problems that the other posters mentioned.) You would import it into your class exactly the same way you import any other class you use. And to use it, you just throw an instance of it when some suitable condition arises. Like for example
Michael Hubele
Ranch Hand

Joined: Dec 17, 2005
Posts: 182
So what should the exception look like should both constructors have super in it or what?
Michael Hubele
Ranch Hand

Joined: Dec 17, 2005
Posts: 182
Originally posted by Paul Clapham:
Yes, it is just that easy to write an Exception class That is all you have to do. (Apart from the problems that the other posters mentioned.) You would import it into your class exactly the same way you import any other class you use. And to use it, you just throw an instance of it when some suitable condition arises. Like for example


So since I wrote something in my exception right I don't need to put this:

if (code.length != 4) {
throw new CodeLengthOutOfBoundsException("stuff here right");

since that would be kind of redundent
John Simpson
Ranch Hand

Joined: Nov 28, 2005
Posts: 30
Having a constructor in the exception that accepts a message string, as well as a default constructor gives greater flexibility for future use.
For instance if you want to check a field length of 5 in another scenario, you can re-use the same exception, and just pass a different message. Also if you need to change the code.length for any reason, you don't have to change the exception.

John
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

This is what I was talking about:



So if the programmer supplies a message, it's used; if they don't the default message is used instead.
 
Consider Paul's rocket mass heater.
 
subject: Writing Your Own Exceptions