Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

user defined exception

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.

 
this llama doesn't want your drama, he just wants this tiny ad for his mama
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic