Java基础

异常处理

使用try…catch语句

try {
    // 可能抛出异常的代码块
    // 在这里编写可能引发异常的代码
} catch (ExceptionType1 e1) {
    // 处理 ExceptionType1 异常的代码
    // 可以使用 e1 对象来访问异常信息
} catch (ExceptionType2 e2) {
    // 处理 ExceptionType2 异常的代码
    // 可以使用 e2 对象来访问异常信息
} finally {
    // 可选的 finally 块
    // 无论是否发生异常,这里的代码都会执行
}

注意,这些ExceptionType之间如果包含继承关系的话,子在前,父在后。

throws

使用 throws 关键字来声明一个方法可能抛出的异常。这允许你在方法签名中明确指定某个方法可能抛出的异常类型,而不是在方法内部捕获异常。这样做的目的是将异常的处理责任委托给调用该方法的代码。

throws 关键字的一般用法如下:

returnType methodName(parameterList) throws ExceptionType1, ExceptionType2, ... {
    // 方法体
}

以下是一个示例,演示了如何在方法中使用 throws 关键字抛出异常:

public class ExceptionThrowingExample {
    public static void main(String[] args) {
        try {
            int result = divide(10, 0);
            System.out.println("Result: " + result);
        } catch (ArithmeticException e) {
            System.err.println("An error occurred: " + e.getMessage());
        }
    }

    public static int divide(int dividend, int divisor) throws ArithmeticException {
        if (divisor == 0) {
            throw new ArithmeticException("Division by zero is not allowed.");
        }
        return dividend / divisor;
    }
}

在上述示例中,divide 方法在其方法签名中使用 throws ArithmeticException 声明,这表示该方法可能会抛出 ArithmeticException 异常。在方法内部,如果 divisor 为零,它会显式地创建一个 ArithmeticException 异常对象并将其抛出,以通知调用者出现了除以零的错误。

使用 throws 关键字可以让方法的调用者知道该方法可能引发的异常,以便他们可以适当地处理或捕获这些异常。在实际开发中,通常会在方法签名中列出可能抛出的异常类型,以便提供清晰的文档和错误处理指南。

throw关键字

throw 关键字用于显式地抛出异常。使用 throw 关键字,你可以在程序中指定一个异常对象,从而告诉程序出现了异常情况,这个异常对象会被传递给异常处理机制,然后程序可以相应地处理该异常。以下是 throw 关键字的一般用法:

throw throwable;

其中 throwable 是一个表示异常的对象,通常是 Exception 或其子类的实例。通过 throw 关键字,你可以抛出自定义异常或标准异常类的实例。

以下是一个示例,演示如何使用 throw 关键字抛出自定义异常:

class CustomException extends Exception {
    public CustomException(String message) {
        super(message);
    }
}

public class ThrowKeywordExample {
    public static void main(String[] args) {
        try {
            int age = -5;
            if (age < 0) {
                throw new CustomException("Age cannot be negative");
            }
            System.out.println("Age: " + age);
        } catch (CustomException e) {
            System.err.println("An error occurred: " + e.getMessage());
        }
    }
}

在这个示例中,CustomException 是一个自定义异常类,它继承自 Exception。在 main 方法中,如果年龄 age 的值为负数,就会使用 throw 关键字抛出一个 CustomException 异常,其中包含错误消息。然后,异常会在 catch 块中捕获和处理。

需要注意的是,throw 关键字通常与 trycatch 一起使用,以便在异常情况下进行适当的处理。通过 throw 关键字,你可以在程序中明确指定何时抛出异常,以便更好地控制和管理异常情况。

throw和throws的区别

throw用于方法体内部,用来显式地抛出一个异常对象
throws用户方法声明,用来指定方法可能抛出的异常类型

以下是使用 throwthrows 的两个简单示例,分别展示了它们的不同用法:

  1. 使用 throw 抛出自定义异常:
class CustomException extends Exception {
    public CustomException(String message) {
        super(message);
    }
}

public class ThrowExample {
    public static void main(String[] args) {
        try {
            int age = -5;
            if (age < 0) {
                throw new CustomException("Age cannot be negative");
            }
            System.out.println("Age: " + age);
        } catch (CustomException e) {
            System.err.println("An error occurred: " + e.getMessage());
        }
    }
}

在上面的示例中,使用 throw 关键字抛出了一个自定义异常 CustomException,其中包含错误消息。这个异常会在 catch 块中捕获和处理。

  1. 使用 throws 在方法声明中指定异常类型:
class CustomException extends Exception {
    public CustomException(String message) {
        super(message);
    }
}

public class ThrowsExample {
    public static void main(String[] args) {
        try {
            someMethod(-5);
        } catch (CustomException e) {
            System.err.println("An error occurred: " + e.getMessage());
        }
    }

    public static void someMethod(int age) throws CustomException {
        if (age < 0) {
            throw new CustomException("Age cannot be negative");
        }
        System.out.println("Age: " + age);
    }
}

在上面的示例中,使用 throws 关键字在 someMethod 方法的声明中指定了可能抛出的异常类型 CustomException。这表示调用 someMethod 方法的代码应该考虑处理 CustomException 异常。在 someMethod 方法内部使用 throw 抛出了自定义异常。

这两个示例展示了 throwthrows 的不同用法。throw 用于在方法内部显式抛出异常,而 throws 用于方法声明中指定可能抛出的异常类型。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值