• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating a new exception  RSS feed

 
Ron Gambill
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a new Java programmer who's trying to learn the ins and outs of the Java programming language, so my question is not necessarily of a practical put-in-place nature, but more of a I-want-to-know.

I've done some searching and a lot of reading in my books, but I can't find a really good example of creating a new exception. I've found how to implement (try and catch, etc.), how to create a class for the exception extending the already existing Exception.

But where do I put my new class so that I could utilize it? I've created Beans and placed them in their own directory, got the web server & jsps to find them. But it seems a little different when creating an exception class.

Any insight or direction-pointing would be helpful.
-Ron

BTW, I'm using Tomcat 5.0, JDK 1.4.2.

[My learning example is checking a request.getParameter for null and sending a user to an error page. Like I said, not realistic, just something "simple" for learning purposes only.]
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To create your own exception, just extend java.lang.Exception for a checked exception or java.lang.RunimeException for an unchecked exception. You'll probably want both a no-arg constructor and a constructor that takes a String with a message. You can pass messages up with super(message);

Another question is where to put the .class file for general use. Any directory in your CLASSPATH will do, but there may be standards for user libraries, I'm not sure.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to add to what Mike said, you will also want a constructor that takes a String and a Throwable to allow you to nest exceptions.

After that, its treated just like a regular class -- except that as a subclass of Throwable, it can be thrown and caught.
 
Ron Gambill
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It wasn't the actual exception creation that had me befuddled and resulted in this post (although I spent a lot of time researching this too). After doing quite a bit of searching after this post, I ran across a couple of not-so-easy-to-find posts on another site that finally explained that you must "package" everything.

I just tried to create a class without a "package" declaration, and it wasn't working.

I don't know if this is just a Tomcat thing, or if all others require a class to be in a package/directory as in: app/WEB-INF/classes/PackageName.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should ALWAYS use packages.
Despite the JLS allowing you to have classes outside a package it's very bad practice.
Books often use it for brevity but apart from that it's not to be used.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!