• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Writing Your Own Exceptions

 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Michael Hubele
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what should the exception look like should both constructors have super in it or what?
 
Michael Hubele
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic