第3章:最常见的异常是什么?它们是什么意思?
正如我们在上一章中看到的那样,不仅有标准的Exception,而且还有一些特殊的异常,例如
NullPointerException或ArrayIndexOutOfBoundsException 。 所有这些扩展了基本类Exception 。通常,您可以将异常分为两类:已检查和未检查的异常。 Checked Exception由编译器在编译时检查。 您将用来控制程序流程的大多数异常将被选中。
未经检查的异常会在运行时引发,通常意味着您在某个地方犯了一个错误,但编译器不知道这一点。
所有未检查的异常都会扩展
RuntimeException ,而所有选中的Exception均不。有一些例外,您会经常遇到。 我已尝试列出最常见的清单,并解释您可以找到的内容,来源。 您应该始终查看从编译器或运行时获得的消息-它会为您提供遇到错误的那一行。
- ArrayIndexOutOfBoundsException
就像他们大多数人都应该做的那样,这个异常描述了哪里出了错-某些Array索引超出了给定范围。 因此,例如,您有一个像这样的数组
并且您正在尝试访问不存在的元素,也许像这样int [] array = new int[3]
这是不正确的,因为数组以元素0开头。array[3]
使用经常运行的for循环时,经常会出现此错误。 检查给定的行,找出为什么它试图找到一个不存在的元素。
ArrayIndexOutOfBoundsException是未经检查的异常。
- ClassCastException
如果遇到ClassCastException,则某个地方存在无效的强制转换。 例如,以下代码引发ClassCastException:
这样做是因为它无法将Class1对象转换为Class2对象。 如果您自己创建了类,则通常必须告诉编译器如何将它们强制转换为另一个类:public class Exceptions { public static void main(String[] args) { // Anything extends java.lang.Object directly or indirectly and is therefore an Object Object testClass1 = new Class1("1"); Object testClass2 = (Class2) testClass1; } } class Class1 { String x; public Class1(String x) { this.x = x; } } class Class2 { int x; public Class2(int x) { this.x = x; } }
ClassCastException是未经检查的异常。public class Exceptions { public static void main(String[] args) { // Now we've defined the cast ourselves and don't need to define the new Objects as Objects. Class1 testClass1 = new Class1("1"); Class2 testClass2 = testClass1.toClass2(); // If we do want to define them as Objects, it would work like this: Object testClass1_2 = new Class1("1"); Object testClass2_2 = ((Class1) testClass1_2).toClass2(); } } class Class1 { String x; public Class1(String x) { this.x = x; } public Class2 toClass2() { return new Class2(Integer.parseInt(x)); } } class Class2 { int x; public Class2(int x) { this.x = x; } }
- ClassNotFoundException
这意味着找不到某个类。 通常,当您编译类并尝试运行它们而不编译其他需要的类时,通常会发生这种情况。 这也可能意味着所需的类已编译,但不是最新的。 尝试重新编译所需的一切。
- FileNotFoundException
当您尝试访问文件时,该文件可能不存在。 此代码将引发FileNotFoundException:
警告:请没有主方法,该方法会引发异常。 仅应出于演示目的进行此操作。 捕获并处理异常。 处理这种情况的更好方法是:import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; public class Exceptions { public static void main(String[] args) throws FileNotFoundException { File file = new File("hello.txt"); FileInputStream iStream = new FileInputStream(file); } }
ClassNotFoundException是已检查的异常。import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; public class Exceptions { public static void main(String[] args){ try { File file = new File("hello.txt"); FileInputStream iStream = new FileInputStream(file); } catch(FileNotFoundException fnfe) { System.err.println(fnfe); } } }
- IOException: Input-Output-Exception是一个异常,这是非常常用的。 FileNotFoundException就是这些之一,因此可以被捕获:
以下代码将引发IOException:catch(IOException ioe) { //... }
这也是两件事的示例:public static void throwIOException() throws IOException { try { OutputStream oStream = new FileOutputStream(new File("")); oStream.close(); oStream.write(0); } // The FileNotFoundException extends IOException - however to get a basic // IOException, we do this: catch(IOException fnfe) { IOException ioe = new IOException(); ioe.initCause(fnfe); throw ioe; } }
- 包装的异常-try-catch-block捕获一个异常并引发另一个异常。 在某些情况下这可能非常有用。
- 创建链接的异常。 这意味着,异常携带有关其来源的信息(即“ FileNotFoundException”)。 此信息的添加是通过ioe.initCause(fnfe);行完成的。 可以保留
catch(IOException ioe) { { System.err.println(ioe); System.err.println("Cause: " + ioe.getCause()); }
- 空指针异常
定义对象后,将抛出NullPointerException,但在尝试访问它时未声明。 要再次使用上一个示例,此代码将引发NullPointerException:
检查是否已声明要在异常给定的行中使用的Object。import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; public class Exceptions { public static File file; public static void main(String[] args){ try { FileInputStream iStream = new FileInputStream(file); } catch(FileNotFoundException fnfe) { System.err.println(fnfe); } } }
NullPointerException是未经检查的异常。
From: https://bytes.com/topic/java/insights/750362-introduction-exceptions-ch-3-a