I have a method like this:
public void getSomething(){
...
}
I want to throw an Exception
inside getSomething()
. The
Java has two kinds of exceptions, checked and unchecked. You must declare checked exceptions, but you don't have to declare unchecked exceptions.
RuntimeException
is the basic unchecked exception, so you can throw that without declaring it.
public void getSomething(){
throw new RuntimeException("I don't have to be declared in the method header!");
}
As a side note, you probably don't want to throw a raw RuntimeException, but subclass it to something more specific to your needs. Any subclass of RuntimeException will be unchecked as well.