Black monkey house: Scala abnormal

When abnormal conditions encountered, method throws an exception, the execution termination method itself, exceptions to its caller, the caller can handle the exception, it can be upgraded to its caller. Runtime upgrade would have been so abnormal, until the caller can handle it. If you have not been processed, the entire program is terminated.

Scala abnormal working mechanisms and Java, but Scala does not "checked" exception, you do not need the statement said function or method may throw some kind of exception. Unchecked exceptions are checked by the compiler, java exception type must declare method will be thrown.

Throws an exception: The keyword throw, throw an exception object. All exceptions are subtypes of Throwable. throw expression is typed, it is Nothing, because Nothing is a subtype of all types, so throw expression can be used in place of the type required.

Catch exceptions: In Scala, the borrowed idea to do pattern matching unusual match, therefore, the catch code, the case is a series of words.

Abnormal capture mechanism like any other language, if there is an exception occurs, catch words are sequential capture. Thus, in the words of the catch, the more specific exception Echizen rely on, the more the more common abnormalities by. If an exception is not catch the words, the abnormality can not be processed and will be upgraded to the caller at.

finally, whether the words used to perform normal processing steps still need to be performed when an exception occurs, generally used for clean-up objects.

1, Java abnormal wording

unchecked exceptions and checked exceptions

try{

}catch (NullPointerException e1){

}catch (Exception e2){

}finally {

}

2, Scala abnormal wording

Scala unchecked exceptions only, no checked exceptions

//scala写法
try {

} catch {
  case e1: NullPointerException =>
  case e2: Exception =>
} finally {

}

3, case presentations

object Loop_Exception {
  def main(args: Array[String]): Unit = {
    def divider(x:Double,y:Double) = {
      if(y == 0 ) throw new Exception("0 不能作为除数")
      //if(y == 0 ) throw new NullPointerException("空指针 异常")
      x / y
    }
    println(divider(10,0))

      //Ctrl + Alt + T
      try {
        println(divider(10, 0))
      } catch {
        case e1:Exception => println("0 作为除数的异常被成功捕获" + " -> " + e1.getMessage + " -> " + e1.printStackTrace())
        case e2:Throwable => println("0 作为除数的异常被成功捕获")
        case e3:Any => println("0 作为除数的异常被成功捕获")
        case e4:Object => println("0 作为除数的异常被成功捕获")
      } finally {}

  }
}

4, case supplement

object ExceptionSyllabus {
    def main(args: Array[String]): Unit = {

        def divider(x:Double,y:Double) = {
            if (y == 0) throw new Exception (" || 0 不能作为除数")
                x/y
        }

        //e1 下面的四种类型都可以,只要大于捕获的异常类型就可以
        try {
             println(divider(10, 0))
        } catch {
         //IndexOutOfBoundsException 不是 一个类型 也不是包含关系,空指针是空指针,下标越解是下标越界
         //case e1:IndexOutOfBoundsException => println("0不能作为除数" + e1.getMessage)
         //case e1:NullPointerException => println("0不能作为除数" + e1.getMessage)
         //case e1:Exception => println("0不能作为除数" + e1.getMessage)
         //case e1:Throwable => println("0不能作为除数" + e1.getMessage)
         //case e1:AnyRef => println("0不能作为除数" + e1.getMessage)
          case e1:Any => println("0不能作为除数" + e1.getMessage)
        }
    }
}

Reproduced in: https: //www.jianshu.com/p/3b4e4b3231c2

Guess you like

Origin blog.csdn.net/weixin_33733810/article/details/91182432