Java Exception类的子类详解 - 常见的异常类型与示例
"Exception类的子类有哪些"\n\nException类的子类是Java中处理异常的重要组成部分,它们代表了不同类型的异常情况。以下列出了一些常见的Exception子类,并简要说明它们的用途:\n\n1. ArithmeticException:表示算术运算异常,如除零异常。\n\njava\npublic class ArithmeticExceptionExample {\n public static void main(String[] args) {\n try {\n int result = 10 / 0;\n } catch (ArithmeticException e) {\n System.out.println("ArithmeticException: " + e.getMessage());\n }\n }\n}\n\n\n2. ArrayIndexOutOfBoundsException:表示数组下标越界异常。\n\njava\npublic class ArrayIndexOutOfBoundsExceptionExample {\n public static void main(String[] args) {\n int[] numbers = {1, 2, 3};\n try {\n System.out.println(numbers[3]);\n } catch (ArrayIndexOutOfBoundsException e) {\n System.out.println("ArrayIndexOutOfBoundsException: " + e.getMessage());\n }\n }\n}\n\n\n3. NullPointerException:表示空指针异常。\n\njava\npublic class NullPointerExceptionExample {\n public static void main(String[] args) {\n String str = null;\n try {\n System.out.println(str.length());\n } catch (NullPointerException e) {\n System.out.println("NullPointerException: " + e.getMessage());\n }\n }\n}\n\n\n4. ClassCastException:表示类转换异常。\n\njava\npublic class ClassCastExceptionExample {\n public static void main(String[] args) {\n Object obj = new Integer(10);\n try {\n String str = (String) obj; // 抛出ClassCastException\n } catch (ClassCastException e) {\n System.out.println("ClassCastException: " + e.getMessage());\n }\n }\n}\n\n\n5. IllegalArgumentException:表示非法参数异常。\n\njava\npublic class IllegalArgumentExceptionExample {\n public static void main(String[] args) {\n try {\n calculate(10, -1); // 负数参数是非法的\n } catch (IllegalArgumentException e) {\n System.out.println("IllegalArgumentException: " + e.getMessage());\n }\n }\n public static void calculate(int a, int b) {\n if (b <= 0) {\n throw new IllegalArgumentException("参数b必须大于0");\n }\n // ...\n }\n}\n\n\n6. FileNotFoundException:表示文件未找到异常。\n\njava\nimport java.io.File;\nimport java.io.FileNotFoundException;\nimport java.util.Scanner;\n\npublic class FileNotFoundExceptionExample {\n public static void main(String[] args) {\n try {\n File file = new File("nonexistent.txt");\n Scanner scanner = new Scanner(file); // 抛出FileNotFoundException\n } catch (FileNotFoundException e) {\n System.out.println("FileNotFoundException: " + e.getMessage());\n }\n }\n}\n\n\n7. IOException:表示输入输出异常。\n\njava\nimport java.io.File;\nimport java.io.FileNotFoundException;\nimport java.io.IOException;\nimport java.io.PrintWriter;\n\npublic class IOExceptionExample {\n public static void main(String[] args) {\n try {\n File file = new File("output.txt");\n PrintWriter writer = new PrintWriter(file); // 抛出IOException\n writer.println("Hello, world!");\n writer.close();\n } catch (IOException e) {\n System.out.println("IOException: " + e.getMessage());\n }\n }\n}\n\n\n8. InterruptedException:表示线程中断异常。\n\njava\npublic class InterruptedExceptionExample extends Thread {\n public void run() {\n try {\n Thread.sleep(1000); // 可能会被中断\n } catch (InterruptedException e) {\n System.out.println("InterruptedException: " + e.getMessage());\n }\n }\n public static void main(String[] args) {\n InterruptedExceptionExample thread = new InterruptedExceptionExample();\n thread.start();\n thread.interrupt(); // 中断线程\n }\n}\n\n\n9. RuntimeException:表示运行时异常的基类,包括诸如数组越界、空指针等常见的运行时异常。\n\n注意: 运行时异常通常不需要显式处理,因为它们在运行时才会被抛出,编译器不会强制要求你捕获它们。\n\n本文只列出了部分常见的Exception子类,更多子类信息可以参考Java官方文档。\n\n在编写Java代码时,理解不同类型的异常以及如何处理它们非常重要。合理处理异常可以使程序更健壮,更易于维护。\n\n希望本文能帮助你更好地理解Java中的异常处理机制。\
原文地址: https://www.cveoy.top/t/topic/pL0a 著作权归作者所有。请勿转载和采集!