• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating a Runtime Exception  RSS feed

 
Olivier Legat
Ranch Hand
Posts: 176
Chrome Mac Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

How can I create my own Runtime exceptions so that I don't have to write throws Exception when I write a method.

Example:



Is it possible to modify my NotACardException so that the syntax of Card is correct instead of putting throws NotACardException?

Thanks in advance.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I create my own Runtime exceptions so that ...


Have your exception extend the RuntimeException class.

Henry
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although you should see if one of the existing exceptions can be used. In this case, IllegalArgumentException seems to fit just perfectly, and even IndexOutOfBoundsException.
 
Olivier Legat
Ranch Hand
Posts: 176
Chrome Mac Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thank-you
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!