Scala Try Catch Finally
Scala Try Catch Finally:
在Java中返回值优先级顺序:finally最高, try,catch 选其一,try中抛异常,返回catch,不抛异常,返回try,。
public class ExceptionDemo { public static void main(String[] args) { System.out.println(callMD()); } public static int callMD() { try { // throw new Exception(); return 1; } catch (Exception e) { return 2; } finally { return 3; } } }
Scala模仿Java:
object ExceptionCatchDemo extends App { def testTryCatchFinally: Int = { try { throw new Exception();} catch { case _ => return 2 } finally { return 3 } } println(testTryCatchFinally) }
运行结果:3
在Scala中返回值优先级顺序:try,catch 选其一,try中抛异常,返回catch,不抛异常,返回try,finally最低
object ExceptionCatchDemo extends App { def testTryCatchFinally: Int = { try { throw new Exception() } catch { case _ => 2 } finally { 3 } } println(testTryCatchFinally) }
运行结果:2
相关推荐
匆匆那些年 2020-10-15
TheBigBlue 2020-07-28
shenwenjie 2020-07-07
muhongdi 2020-07-07
waitwolf 2020-07-08
yunfenglee 2020-07-08
yunfenglee 2020-07-08
kekeromer 2020-07-08
匆匆那些年 2020-07-07
liqinglin0 2020-07-05
TheBigBlue 2020-07-05
kekeromer 2020-06-13
zhixingheyitian 2020-06-08
TheBigBlue 2020-06-06
liqinglin0 2020-06-01
liqinglin0 2020-06-01
yunfenglee 2020-05-30
MICKEYSTUDY 2020-05-28
muhongdi 2020-05-19