1. If you define a method as returning a String, every normal exit from the method must return a String. Just reaching the final brace is the same as having plain return; as your last statement, which violates your contract to return a String.
2,3. After dividing by zero, you will always leave the catch clause abnormally without reaching the [return " error ";] statement. The question is what happens then. If the finally clause is empty, the program will terminate with a stack trace printed unless there is a higher enclosing try/catch statement. If you transfer control with a return statement, you are telling Java that you don't want to terminate and control continues with the calling method, which prints "peace".
I think you are a bit confused about the divide error message. It isn't printed when the divide error occurs. You could do that yourself in your catch clause but Java won't print anything then. The error message is printed as part of the stack trace after your program terminates abnormally. If you cancel the abnormal termination by issuing a return statement in the finally clause, there will be no stack trace and no error message from Java.
SCJP 1.4, SCWCD in process
posted 14 years ago
Thank you Mike, very nice and clear explanation.
"Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
Everybody's invited. Except this tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java