This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Keeping Exceptions Classes

 
You Gin
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well yeah, this sounds like an option also. Thanks guys.
 
Stephan van Hulst
Bartender
Pie
Posts: 5553
53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll accept your correction
 
Mina Daoud
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic