异常练习

11.2

代码部分:

package lianxi;
import javax.swing.*;
public class bo {

public static void main(String[] args) {

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");
}
}
}

 运行结果:

 

运行结果分析:

代码报错;显示除零错误;

 

posted @ 2020-11-02 21:26  潘福龙  阅读(45)  评论(0编辑  收藏  举报