本文共 5083 字,大约阅读时间需要 16 分钟。
异常是Java编程中不可或缺的一部分,它用来表示程序执行过程中出现的问题。理解异常的机制,有助于我们更好地开发和调试代码。
异常是一种导致程序中断的指令流。它可以是编译时的错误,也可以是运行时的错误。如果不对异常进行正确的处理,就可能导致程序无法正常运行,甚至引发中断指向,造成不必要的损失。
Throwable是所有异常的父类。它包括了两种主要的异常类型:
Error表示严重的问题,这些问题通常是由Java Virtual Machine(JVM)本身抛出的。它们通常不会被程序主动处理,而是需要进行故障转换或报告。
Exception和它的子类表示程序中的错误或异常。Exception可以分为两种类型:
RuntimeException及其子类都是运行时异常。这种异常不需要声明在方法签名中,可以直接在代码中抛出。然而,由于它不需要处理,程序在执行时会抛出错误。
除了RuntimeException,其他异常被称为编译时异常。这些必须在编译时声明,并且必须在代码中进行处理,否则程序无法运行。
异常处理的结构通常采用try-catch-finally的形式。try块中包含可能抛出异常的代码,catch块处理异常,finally块执行资源释放操作。最终的代码示例如下:
public class ExceptionTest { public static void main(String[] args) { try { System.out.println("结果:" + 10 / 0); }catch (ArithmeticException e) { System.out.println("除数不能为0"); }finally { System.out.println("程序结束"); } }} try可包含多个catch块。每个catch对应一种异常类型。例如:
try { System.out.println(10 / 0); System.out.println("数组越界");} catch (ArithmeticException e) { System.out.println("算术异常");} catch (ArrayIndexOutOfBoundsException e) { System.out.println("数组越界");}finally { System.out.println("程序结束");} try { // 可能抛出多种异常} catch (Throwable t) { System.out.println("捕获到异常");} 优点:
缺点:
从JDK7开始,异常处理更加简化。可以将同级异常合并处理:
try { // 可能抛出的异常} catch (ArrayIndexOutOfBoundsException | ArithmeticException e) { System.out.println("异常处理");} Throwable 是所有异常和错误的基类。它提供了获取异常信息的方法。例如:
public class ExceptionDemo4 { public static void main(String[] args) { try { System.out.println(10 / 0); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } }} throws用于方法定义时声明可能抛出的异常。它本身不会抛出异常,而是将责任转交给调用者。例如:
public class throwsTest { public static void main(String[] args) { try { method(); } catch (Exception e) { e.printStackTrace(); } } public static void method() throws Exception { System.out.println(10 / 0); }} throw用于在方法体内抛出已定义的异常。它必须明确抛出一个异常对象。例如:
public class throwDemo { public static void main(String[] args) { method(); } public static void method() { throw new ArithmeticException(); }} 如果我们可以处理异常,就应该使用try-catch-finally结构。例如:
public class ResourceDemo { public static void main(String[] args) { BufferedReader reader = null; try { reader = new BufferedReader(new FileReader("文件.txt")); System.out.println(reader.readLine()); } catch (IOException e) { System.out.println("错误读取文件"); } finally { if (reader != null) { reader.close(); } } }} 如果我们确定方法可能抛出的异常,应该在方法签名中声明,并让调用者自己处理。例如:
public class throwsDemo { public static void main(String[] args) throws IOException { String路径 = "文件路径"; Reader reader = new BufferedReader(new FileReader(路径)); System.out.println(reader.readLine()); }} finally块总是执行,无论是否发生异常或错误。它的主要作用是释放资源。例如:
public class finallyDemo { public static void main(String[] args) { try { System.out.println(10 / 0); }catch (Exception e) { System.out.println("无法处理除数为0的情况"); }finally { System.out.println("程序结束执行"); } }} 是的,finally块在return前执行。例如:
public class finallyDemo2 { public static void main(String[] args) { System.out.println(method()); } public static int method() { try { System.out.println(10 / 0); }catch (Exception e) { return 200; }finally { System.out.println(400); } return 0; }} 我们可以创建自定义异常类,继承Exception或RuntimeException。例如:
public class MyException extends Exception { public MyException(String message) { super(message); }}public class Teacher { public void check(int score) throws MyException { if (score > 100 || score < 0) { throw new MyException("成绩必须在0到100之间"); } else { System.out.println("成绩有效"); } }}public class Student { public static void main(String[] args) throws MyException { Scanner输入 = new Scanner(System.in); int 分数 =输入.nextInt(); Teacher 老师 = new Teacher(); 老师.check(分数); }} 理解异常机制是编写高质量Java代码的核心能力之一。通过合理使用try-catch-finally结构,并正确处理异常、自定义异常,可以显著提高程序的健壮性和稳定性。在实际开发中,除了掌握理论知识,还需要多看代码案例,理解不同异常类型的处理方式,从而灵活应对各种开发场景。
转载地址:http://gkpez.baihongyu.com/