Scala exceptions (Chapter 9: The difference between java exception handling and Scala exception handling)


abnormal

语法处理上和 Java 类似,但是又不尽相同。

9.1 Java exception handling

public class ExceptionDemo {
    
    
 public static void main(String[] args) {
    
    
 try {
    
    
 int a = 10;
 int b = 0;
 int c = a / b;
 }catch (ArithmeticException e){
    
    
// catch 时,需要将范围小的写到前面
 e.printStackTrace();
 }catch (Exception e){
    
    
 e.printStackTrace();
 }finally {
    
    
 System.out.println("finally");
 }
 }
}

Precautions

  • (1) The Java language handles exceptions in a try-catch-finally manner.
  • (2) Finally will be executed regardless of whether there is exception capture, so resources can usually be released in the finally code block.
  • (3) There can be multiple catches to capture corresponding exceptions respectively. In this case, you need to write the exception class with a small scope in front.
    Write the exception class with a large scope in later, otherwise there will be a compilation error.

9.2 Scala exception handling

def main(args: Array[String]): Unit = {
    
    
 try {
    
    
 var n= 10 / 0
 }catch {
    
    
 case ex: ArithmeticException=>{
    
    
 // 发生算术异常
 println("发生算术异常")
 }
 case ex: Exception=>{
    
    
 // 对异常处理
 println("发生了异常 1")
 println("发生了异常 2")
 }
 }finally {
    
    
 println("finally")
 }
}
  • 1) We encapsulate the suspicious code in a try block. A catch handler is used after the try block to catch exceptions. If any exception occurs, the catch handler will handle it and the program will not terminate abnormally.
  • 2) The working mechanism of Scala's exceptions is the same as that of Java, but Scala does not have "checked (compile time)" exceptions, that is, Scala does not have the concept of compile exceptions, and exceptions are captured and processed at runtime.
  • 3) The exception catching mechanism is the same as in other languages. If an exception occurs, the catch clause is caught in order. Therefore, in the catch clause, the more specific exceptions should be placed first, and the more common exceptions should be placed later. If the more general exceptions are written first and the specific exceptions are written last, in Scala< a i=1> will not report an error, but this is a very bad programming style.
  • 4) The finally clause is used to perform steps that need to be performed whether it is normal processing or when an exception occurs. It is generally used for object cleanup, which is the same as Java.
  • 5) Use the throw keyword to throw an exception object. All exceptions are subtypes of Throwable. The throw expression has a type, which is
    Nothing. Because Nothing is a subtype of all types, the throw expression can be used where a type is required.
def test():Nothing = {
    
    
 throw new Exception("不对")
}
  • 6) Java provides the throws keyword to declare exceptions. Exceptions can be declared using method definitions. It provides the caller function with information that this method may throw this exception. It helps to call function processing and include that code in a try-catch block to avoid abnormal termination of the program. In Scala, you can use the throws annotation to declare exceptions
def main(args: Array[String]): Unit = {
    
    
 f11()
}
@throws(classOf[NumberFormatException])
def f11()={
    
    
 "abc".toInt
}

Guess you like

Origin blog.csdn.net/Argonaut_/article/details/129167448