Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

self defined Exception  RSS feed

 
Timothy Leong
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear all,

I would like to self defined an exception. Let's said I have an array of 20 Person objects. What exception do I need to extend from to warn the user tt duplicate records(eg : same ID) are read into the array?

and when I retrieve the Person's address from a file, I would want to limit to no longer than 20 characters, how shld I define the exception class?

My main problem is I am not sure what are the exceptions I need to extend from. Thank guys
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generally, you'd just extend "Exception" (if you want your exception to be a "checked" one -- the kind you're forced to catch) or "RuntimeException" (if you want to allow the caller to ignore the exception and let it propagate, undeclared.) It's rather uncommon for custom exception classes to extend more specific API exception types.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!