java版
package org.funmo.java;
public class HelloJava {
public static void main(String[] args) {
int i = 0;
int j = 1;
try {
int k = j / i;
} catch (ArithmeticException e) {
System.out.println(e);
} catch (Exception e) {
System.out.println(e);
} finally {
System.out.println("finally ! ");
}
}
}
scala版
package org.funmo.scala
object HelloScala {
def main(args: Array[String]) {
val i = 0
val j = 1
try {
j / i
} catch {
case e: ArithmeticException => printf("the exception is %s \n", e)
case e: Exception => printf("the exception is %s \n", e)
} finally {
println("finally ! ")
}
}
}
对比发现scala的异常处理case方式貌似更加美观。