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

Need for Custom Exceptions??

A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,

Why do we go in for custom exceptions...???

Tx
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14107
    
  16

Because the exception classes that you get in the standard Java library are not always exactly what you want. In a real-world application, you'll probably want to make some custom exception classes for specific error conditions that can happen in your business logic.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Thanks...Jesper!!!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14107
    
  16

For example: I'm working in a project now where we're writing software for a railway company. The software contains some business rules that have to do with how trains travel from one station to the next. We have a database in which the state of all the trains is kept. All trains are identified by a train number.

Suppose that in the GUI somebody enters an invalid train number. When the software tries to lookup the train in the database and it can't find it, it throws a TrainNotFoundException.

That exception gets caught higher up in the application and the user gets a warning in the GUI that (s)he entered an invalid train number.

In fact, the TrainNumberNotFoundException is a subclass of BusinessLogicException, which extends Exception. Instead of catching TrainNotFoundException, the code catches BusinessLogicException, to handle not only train not found exceptions but all kinds of business logic exceptions.
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Thanks once again Jesper !!!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You might also want to add custom functionality. We built chained exceptions before the language supported it. On one project we saved the stack trace as a private String so it would be serialized across network calls where the normal stack trace info is "transient" and is discarded by serialize. Another project supported multiple levels of messages: a short one that appears on screen, a longer one that explains the problem, and the original technical message that is useful only to support folks. Probably too much, eh?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need for Custom Exceptions??