• 今日收获


    1

    import javax.swing.*;
    
    class AboutException {
       public static void main(String[] a) 
       {
          int i=1, j=0, k;
          k=i/j;
    
    
        try
        {
            
            k = i/j;    // Causes division-by-zero exception
            //throw new Exception("Hello.Exception!");
        }
        
        catch ( ArithmeticException e)
        {
            System.out.println("被0除.  "+ e.getMessage());
        }
        
        catch (Exception e)
        {
            if (e instanceof ArithmeticException)
                System.out.println("被0除");
            else
            {  
                System.out.println(e.getMessage());
                
            }
        }
    
        
        finally
             {
                 JOptionPane.showConfirmDialog(null,"OK");
             }
            
      }
    }

    try里面放的是可能出现错误的代码

    如果出错则跳转到catch里处理

    不管有没有错误都会执行finally

    如果没有提供合适的异常代码处理代码,JVM将会结束掉整个应用程序.

    2.可以有多个catch语句块,每个代码块捕获一种异常,在某个try块后有两个不同的catch块补货两个相同类型的异常是语法错误。使用catch语句,只能捕获Exception类及其子类的对象。因此,一个捕获exception对象的catch语句块可以捕获所有的“可捕获”的异常。

    将catch(Exception e)放在别的catch块前面会使这些catch块都不执行,因此Java不会编译这个程序.

  • 相关阅读:
    Binary Tree Inorder Traversal
    Populating Next Right Pointers in Each Node
    Minimum Depth of Binary Tree
    Majority Element
    Excel Sheet Column Number
    Reverse Bits
    Happy Number
    House Robber
    Remove Linked List Elements
    Contains Duplicate
  • 原文地址:https://www.cnblogs.com/feng747/p/13916264.html
Copyright © 2020-2023  润新知