0

This is my code:

public boolean SetYear (String year)
{
		if (year.matches("[0-9]{4}"))
		{ 
		_year=year;
		return true;
                }
    return false;
}

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?

5
Contributors
9
Replies
10
Views
8 Years
Discussion Span
Last Post by Ezzaral
0

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

0

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.

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.