错误:在我们编写程序的过程中会经常发生的,包括编译期间和运行期间的错误。
异常:在程序运行过程中,意外发生的情况,背离我们程序本身的意图的表现,都可以理解为异常。
通过五个关键字来实现:try、catch、finally、throw(手动抛出异常,通常配合try catch或throws使用)、throws
案例如下:
public class TestMain { public void compute() { int x = 10, y = 0; double z = 0; try { z = x / y; System.out.println(z); } catch (ArithmeticException e) { System.out.println("除数不得为0"); } catch (Exception e) { // 通常会在最后加上这个,来处理可能被遗漏的异常 e.printStackTrace(); } } public static void main(String[] args) { TestMain testMain = new TestMain(); testMain.compute(); } }案例如下
public class TestMain { public void compute2() throws ArithmeticException{ int x = 10, y = 0; double z = 0; z = x / y; System.out.println(z); } public static void main(String[] args) { TestMain testMain = new TestMain(); try { testMain.compute2(); } catch (Exception e) { System.out.println("除数不得为0"); } } }配合try…catch…
public class TestMain { public void compute3() { int x = 10, y = 0; double z = 0; try { if (y == 0) { throw new Exception("除数不能为0"); }else { z = x / y; System.out.println(z); } } catch (ArithmeticException e) { System.out.println("除数不得为00"); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { TestMain testMain = new TestMain(); testMain.compute3(); } } /* 抛出异常: java.lang.Exception: 除数不能为0 */配合throws
public class TestMain { public void compute4() throws Exception{ int x = 10, y = 0; double z = 0; if (y == 0) { throw new Exception("除数不能为0"); }else { z = x / y; System.out.println(z); } } public static void main(String[] args) { TestMain testMain = new TestMain(); try { testMain.compute4(); } catch (Exception e) { System.out.println("除数不得为0"); } } } /* 运行结果: 除数不得为0 */取用上面的案例
public class DivException extends Exception { public DivException() { super("除数不得为0"); } } public class TestMain { // throw与try catch public void compute3() { int x = 10, y = 0; double z = 0; try { if (y == 0) { throw new DivException(); // 抛出自定义异常 }else { z = x / y; System.out.println(z); } } catch (ArithmeticException e) { System.out.println("除数不得为00"); } catch (Exception e) { e.printStackTrace(); } } public void compute4() throws Exception{ int x = 10, y = 0; double z = 0; if (y == 0) { throw new DivException(); // 抛出自定义异常 }else { z = x / y; System.out.println(z); } } // throw与throws搭配 public static void main(String[] args) { TestMain testMain = new TestMain(); testMain.compute3(); try { testMain.compute4(); } catch (Exception e) { System.out.println("除数不得为0"); } } }异常链:将异常发生的原因一个传一个串起来,即将底层异常信息传给上层,这样逐层抛出。
public class TestMain { public void compute() throws DivException{ int x = 10, y = 0; double z = 0; try { z = x / y; System.out.println(z); } catch (ArithmeticException e) { throw new DivException(); } } public void testExcept1() throws Exception{ try { compute(); } catch (Exception e) { throw new Exception("新异常1", e); } } public void testExcept2() throws Exception{ try { testExcept1(); } catch (Exception e) { throw new Exception("新异常2", e); } } public static void main(String[] args) { TestMain testMain = new TestMain(); try { testMain.testExcept2(); } catch (Exception e) { e.printStackTrace(); } } }运行结果:
如果将代码中的y = 0改成y = 2,则运行结果为5.0。