This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes user defined exception Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "user defined exception" Watch "user defined exception" New topic
Author

user defined exception

ganapathy gopinathan
Ranch Hand

Joined: Aug 10, 2004
Posts: 30
Hi All,

I have a doubt, in user defined exception.
Is that necessary in the user defined exception, that we should have both the error code and error message as attributes? Or can we have only the error message as attribute?

Thanks in advance!!!
Gopinathan
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24081
    
  15

It's not really necessary to have either (although the Throwable class has a getMessage() method, so if you don't pass a message to your superclass constructor, there will be a message, but it will be an empty one.) In any event, having error codes in user-defined exceptions -- or in any Java exceptions, for that matter -- is rather rare.


[Jess in Action][AskingGoodQuestions]
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884

I have a doubt, in user defined exception.
Is that necessary in the user defined exception, that we should have both the error code and error message as attributes? Or can we have only the error message as attribute?


Are you thinking of using the error code to help identify or classify the types of exceptions? Something similar to what Oracle does? Like whenever we attempt to insert a row with duplicate primary key, an error code would be returned? Or you're thinking along the line of HTTP status codes, the 4xx & 5xx?

The stack trace that comes with the exception would let us know which line the exception was thrown. A descriptive message that we supplied could help us or others that uses our code narrow down or pinpoint the problem.

Also, sometimes, when the name of our custom exceptions are descriptive, just by catching them alone, we would know what went wrong & just have to know which line caused the issue.

Most of the time, a descriptive name & message together with the line number from the stack trace would be sufficient for us to fix the code or handle the exception.



SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: user defined exception
 
Similar Threads
Defining parameterized UserDefinedExceptions in an Application
What Would Cause A Method That Inserts Records To Throw An Exception?
User defined errors
Exceptions
doubt on exception handling