企业网站建设费用入哪个科目,建筑工地老板直招工人,江西机场建设集团网站,做一个手机网站多少钱Java基础异常详解 文章目录 Java基础异常详解编译时异常#xff08;Checked Exception#xff09;#xff1a;运行时异常#xff08;Unchecked Exception#xff09;: Java中的异常是用于处理程序运行时出现的错误或异常情况的一种机制。
异常本身也是一个类。
异常分为…Java基础异常详解 文章目录 Java基础异常详解编译时异常Checked Exception运行时异常Unchecked Exception: Java中的异常是用于处理程序运行时出现的错误或异常情况的一种机制。
异常本身也是一个类。
异常分为两种类型编译时异常Checked Exception和运行时异常Unchecked Exception。
编译时异常Checked Exception
编译时异常是在代码编译阶段就能够被检测到的异常程序员必须在代码中显式处理这些异常否则编译不会通过。常见的编译时异常包括
IOException输入输出异常如文件操作中的读写错误。SQLException数据库访问异常。ClassNotFoundException类未找到异常。InterruptedException线程中断异常等。
处理编译时异常的方式 使用try-catch块捕获异常并处理。 使用throws关键字在方法声明中抛出异常让调用者处理。 以下是一些常见的编译时异常的代码示例 IOException - 输入输出异常 import java.io.*;public class IOExceptionExample {public static void main(String[] args) {try {FileReader fileReader new FileReader(file.txt);// 读取文件内容} catch (IOException e) {System.out.println(文件读取发生异常: e.getMessage());}}
}SQLException - 数据库访问异常 import java.sql.*;public class SQLExceptionExample {public static void main(String[] args) {try {Connection connection DriverManager.getConnection(jdbc:mysql://localhost:3306/mydb, username, password);// 执行数据库操作} catch (SQLException e) {System.out.println(数据库操作发生异常: e.getMessage());}}
}ClassNotFoundException - 类未找到异常 public class ClassNotFoundExceptionExample {public static void main(String[] args) {try {Class.forName(com.example.MyClass);} catch (ClassNotFoundException e) {System.out.println(未找到指定类: e.getMessage());}}
}InterruptedException - 线程中断异常 public class InterruptedExceptionExample {public static void main(String[] args) {Thread thread new Thread(() - {try {Thread.sleep(1000);} catch (InterruptedException e) {System.out.println(线程被中断: e.getMessage());}});thread.start();thread.interrupt();}
}我们通过使用try-catch块来捕获编译时异常并进行处理。
运行时异常Unchecked Exception:
运行时异常是在程序运行时才会被抛出的异常无需在代码中显式处理。如果不处理运行时异常程序将会终止执行。常见的运行时异常包括 ullPointerException空指针异常当尝试访问一个空引用时抛出。 ArrayIndexOutOfBoundsException数组索引越界异常。 IllegalArgumentException非法参数异常当传递非法参数给方法时抛出。 ArithmeticException算术异常如除零操作。 ClassCastException类转换异常当尝试将一个对象强制转换成不兼容的类型时抛出。
运行时异常通常是由程序逻辑错误引起的因此应该通过代码审查和测试来避免。
以下是一些常见的运行时异常的示例代码
NullPointerException - 空指针异常
public class NullPointerExceptionExample {public static void main(String[] args) {String str null;try {int length str.length(); // 这里会抛出NullPointerException} catch (NullPointerException e) {System.out.println(发生空指针异常: e.getMessage());}}
}ArrayIndexOutOfBoundsException - 数组索引越界异常
public class ArrayIndexOutOfBoundsExceptionExample {public static void main(String[] args) {int[] arr {1, 2, 3};try {int value arr[5]; // 这里会抛出ArrayIndexOutOfBoundsException} catch (ArrayIndexOutOfBoundsException e) {System.out.println(发生数组索引越界异常: e.getMessage());}}
}IllegalArgumentException - 非法参数异常
public class IllegalArgumentExceptionExample {public static void main(String[] args) {try {int age -5;if (age 0) {throw new IllegalArgumentException(年龄不能为负数);}} catch (IllegalArgumentException e) {System.out.println(发生非法参数异常: e.getMessage());}}
}ArithmeticException - 算术异常
public class ArithmeticExceptionExample {public static void main(String[] args) {try {int result 10 / 0; // 这里会抛出ArithmeticException} catch (ArithmeticException e) {System.out.println(发生算术异常: e.getMessage());}}
}ClassCastException - 类转换异常
public class ClassCastExceptionExample {public static void main(String[] args) {try {Object obj Hello;Integer num (Integer) obj; // 这里会抛出ClassCastException} catch (ClassCastException e) {System.out.println(发生类转换异常: e.getMessage());}}
}异常处理的方法
try-catch语句块使用try-catch块捕获异常并在catch块中进行处理。
try {// 可能抛出异常的代码
} catch (ExceptionType e) {// 异常处理代码
}throws关键字在方法声明中使用throws关键字抛出异常让调用者处理。
public void methodName() throws ExceptionType {// 方法体
}finally块finally块中的代码无论是否发生异常都会被执行常用于资源的释放等操作。
try {// 可能抛出异常的代码
} catch (ExceptionType e) {// 异常处理代码
} finally {// 最终执行的代码如关闭文件等
}异常处理有助于使程序更健壮能够更好地应对各种异常情况。在处理异常时应根据具体情况选择合适的处理方式以保证程序的正确性和稳定性。