10.17 Java异常处理1

 eg:

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

}
}
此代码一开始的错误就是出现了除0,之后需要try来判断即可
posted @ 2023-10-21 11:36  liuxuechao  阅读(1)  评论(0编辑  收藏  举报