Java面试问题之五
详细分析以下代码段,给出执行结果及其原因。
public class WithReturn{ public int methodA(int money) throws SpecialException{ if(--money<=0) throw new SpecialException("Out of money!!"); return money; } public int methodB(int money){ try{ System.out.println("Begin"); int result=methodA(money); //此处可能抛出异常 return result; }catch(SpecialException e){ System.out.println(e.getMessage()); return -100; }finally{ System.out.println("Finally"); } } public static void main(String[] args){ System.out.println(new WithReturn().methodB(1)); }}
public static int test(){ int a=0; try{ return a; }finally{ a=1; }}public static void main(String[] args) throws Exception{ System.out.println(test());}