码迷,mamicode.com
首页 > 其他好文 > 详细

Scala Try Catch Finally

时间:2018-01-28 21:56:10      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:demo   test   cep   xtend   运行   ret   def   span   int   

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

 

Scala Try Catch Finally

标签:demo   test   cep   xtend   运行   ret   def   span   int   

原文地址:https://www.cnblogs.com/AK47Sonic/p/8372350.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!