Is there any way to exit from the method? I hear that there are two way to exit.
One : throw Exception.
public void dosomething() {
if(...) {
throw new MyException();
}
// there might be another process.
}
Two : return somevalue. Even void method, we can return the value.
public void dosomething() {
if(...) {
return;
}
// there might be another process.
}
Question is, Is there any better way?
Technically you could
System.exit(int)(the int value would be the value returned from the process and likely non-zero to indicate an error). It’s a little brutal, however.You can also interrupt yourself.
However:
InterruptedExceptione.g. (note the
sleep()to catch the interruption):gives me: