943,779 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 702
  • Java RSS
Mar 26th, 2009
0

How can I create a runtime error?

Expand Post »
This is my code:
Java Syntax (Toggle Plain Text)
  1. public boolean SetYear (String year)
  2. {
  3. if (year.matches("[0-9]{4}"))
  4. {
  5. _year=year;
  6. return true;
  7. }
  8. return false;
  9. }

Instead of return false I want to make a runtime error with my massage. Something like "the value must be 4 digits"
How can I do it?
Similar Threads
Reputation Points: 64
Solved Threads: 1
Light Poster
stewie griffin is offline Offline
35 posts
since Jul 2008
Mar 26th, 2009
0

Re: How can I create a runtime error?

Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Mar 26th, 2009
1

Re: How can I create a runtime error?

Specifically, you probably want to throw an IllegalArgumentException. You can set whatever message you want in the constructor.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Mar 27th, 2009
0

Re: How can I create a runtime error?

Or if you want to generate any old runtime error, I've learned plenty of ways to do that in my time programming.

One of the most common is indexing an array by an index too low or too high
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Mar 27th, 2009
-7

Re: How can I create a runtime error?

Define your own exception handler and use try/catch
Moderator
Featured Poster
Reputation Points: 1764
Solved Threads: 574
Moderator
jbennet is offline Offline
16,505 posts
since Apr 2005
Mar 27th, 2009
0

Re: How can I create a runtime error?

Quote ...
Define your own exception handler and use try/catch
Most probably you mean define his own (custom) exception class, but this would be just an overkill.
Ezzaral's suggestion of throwing an IllegalArgumentException for me hits the nail on the head.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Mar 27th, 2009
-7

Re: How can I create a runtime error?

He wants a custom error message so it seems like a better idea
Moderator
Featured Poster
Reputation Points: 1764
Solved Threads: 574
Moderator
jbennet is offline Offline
16,505 posts
since Apr 2005
Mar 27th, 2009
0

Re: How can I create a runtime error?

<Ignore>
Last edited by stephen84s; Mar 27th, 2009 at 6:05 am.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Mar 27th, 2009
0

Re: How can I create a runtime error?

Click to Expand / Collapse  Quote originally posted by jbennet ...
He wants a custom error message so it seems like a better idea
You can set the "message" for most (if not all) inbuilt exceptions including the IllegalArgumentException (for whom this constructor can be used which was also mentioned by Ezzaral in his second post).
Last edited by stephen84s; Mar 27th, 2009 at 6:18 am.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Mar 27th, 2009
0

Re: How can I create a runtime error?

Yes, generally you only need to create a custom exception if you are wanting to pass along additional information that wouldn't be possible with one of the standard exceptions.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: I need help with Program
Next Thread in Java Forum Timeline: Char "[-.!? ,\n]" after space detect as uppercase?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC