package prepare1; class MyMath { static int result;// 想要不加static,就要一开始就给result赋一个值 public static int div(int a, int b) { System.out.println("=====计算开始====="); try { result = a / b; } catch (Exception e) { System.out.println("请检查输入的数字"); throw e;// 把这个异常抛给主方法 } finally { System.out.println("=====计算结束====="); } return result; } } public class Test { public static void main(String args[]) { try { System.out.println(MyMath.div(10, 2)); } catch (Exception e) { e.printStackTrace(); } } }