This is my code:

public boolean SetYear (String year)
		if (year.matches("[0-9]{4}"))
		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?

7 Years
Discussion Span
Last Post by Ezzaral
Featured Replies
  • 1
    Ezzaral 2,714   7 Years Ago

    Specifically, you probably want to throw an [URL="http://java.sun.com/javase/6/docs/api/java/lang/IllegalArgumentException.html"]IllegalArgumentException[/URL]. You can set whatever message you want in the constructor. Read More


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


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.


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 article has been dead for over six months. Start a new discussion instead.
Be sure to adhere to our posting rules.