File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Keeping Exceptions Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Keeping Exceptions Classes" Watch "Keeping Exceptions Classes" New topic
Author

Keeping Exceptions Classes

You Gin
Ranch Hand

Joined: Jul 23, 2010
Posts: 52
Hello,

I've got a basic idea about exceptions recently. There is one thing which I can't get and didn't see in examples is what is a good practice for keeping exception classes, I'm about developer-made exceptions. Is it a good way to write an exception class in the file of the class whose method(s) will invoke this particular exception OR it is better to create a separate file with a list of all exceptions your code can possibly invoke? What is your guys approach on this?

Thank you.
Sudipta Laha
Ranch Hand

Joined: Aug 23, 2010
Posts: 49
Hi,

There are always some benefit to use a separate file for declaring exception.
This can be understood when we have a large codebase and user defined exception. It will not matter so much when you are checking with a small file.

But suppose we create a exception(such as a exception when we insert some invalid values in DB, specific user defined exception we be better than SQL exception to debug). This will be generic for all classes. We will like to have the code to log this exception in a specific file so that we cant rewrite the code again and again.


Regards,
Sudipta Kumar Laha
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
If you want to use the Exceptions in several places in your application, what about an exceptions package with lots of public Exception classes in?
You Gin
Ranch Hand

Joined: Jul 23, 2010
Posts: 52
Well yeah, this sounds like an option also. Thanks guys.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

I disagree Campbell. I think Exceptions should be coupled with the package where they make most sense. Like IOExceptions are in the java.io packages.
It's a bit like making an interface for just constant values. I think it's bad practice.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
I'll accept your correction
Mina Daoud
Ranch Hand

Joined: Sep 24, 2010
Posts: 88
Campbell idea and Stephan one are the best. For all global exception which you will use all over the application, you can build exception package. for specific exceptions which you may use with special classes you declare it in the same package.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Keeping Exceptions Classes