集册 Java实例教程 尝试处理异常

尝试处理异常

欢马劈雪     最近更新时间:2020-01-02 10:19:05

591
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用try-catch块处理异常

public class Main {

  public static void main(String[] args) {

    int x = 10, y = 0, z;
    /** 
    来 自 
    时 代 J a v a - N o w J a v a . c o m
    **/

    try {

      z = x / y;

      System.out.println("z = " + z);

    }

    catch(ArithmeticException e) {

      // Get the description of the exception

      String msg = e.getMessage();

      

      // Print a custom error message

      System.out.println("An error has occurred. The error is: " + msg);

    }

    

    System.out.println("At the end of the program.");

  }

}