Java 中异常的 throws 关键字 [英] The throws keyword for exceptions in Java

查看:23
本文介绍了Java 中异常的 throws 关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当你这样做时:

public class Blah {

    public void doBlah() throws BlahException {

    }

}

添加 throws BlahException 的真正作用是什么?

What does adding the throws BlahException really do?

它是否基本上将任何例外归为一类?即如果有异常,不管它是什么,总是使用 BlahException 抛出?

Does it basically group any exception to that one? i.e. if there is an exception, no matter what it is, will always be thrown using BlahException?

推荐答案

它告诉您的类的客户 DoBlah 方法可以抛出 BlahException 或扩展它的任何其他异常.

It tells the clients of your class that the DoBlah method can throw a BlahException or any other exception that extends it.

如果是受检异常,编译器将要求他们将此方法的调用包装在 try/catch 块中.如果未选中,他们可以选择不捕获异常,但他们必须注意,如果不这样做,它会在调用堆栈中进一步向上冒泡.

If it's a checked exception, the compiler will require that they wrap calls to this method in a try/catch block. If it's unchecked, they can choose to not catch the exception, but they have to be aware that if they don't it'll be bubbled further up the call stack.

它没有说明诸如 NullPointException 或错误之类的未经检查的异常.那些也总是可以抛出的.throws 子句中不需要它们.

It doesn't say anything about unchecked exceptions like NullPointException or errors. Those can always be thrown as well. They aren't required in the throws clause.

这段代码展示了它是如何工作的:

This code shows how it works:

ExceptionDemo.java:

ExceptionDemo.java:

package exceptions;

public class ExceptionDemo
{
    public static void main(String[] args)
    {
        ExceptionDemo demo = new ExceptionDemo();

        try
        {
            // Removing the try/catch will result in a compilation error
            demo.doChecked();            
        }
        catch (CheckedException e)
        {
            e.printStackTrace();
        }

        // Note: Not inside a try/catch, in spite of the throws clause
        demo.doUnchecked();
    }

    public void doChecked() throws CheckedException
    {
        System.out.println("doing something that may throw a checked exception");
    }

    // Note: "throws" clause is unnecessary for an unchecked exception
    public void doUnchecked() throws UncheckedException
    {
        System.out.println("doing something that may throw an unchecked exception");
    }
}

CheckedException.java:

CheckedException.java:

package exceptions;

public class CheckedException extends Exception
{
    public CheckedException()
    {
        super();
    }

    public CheckedException(String message)
    {
        super(message);
    }

    public CheckedException(String message, Throwable cause)
    {
        super(message, cause);
    }

    public CheckedException(Throwable cause)
    {
        super(cause);
    }
}

未检查异常.java:

UncheckedException.java:

package exceptions;

public class UncheckedException extends RuntimeException
{
    public UncheckedException()
    {
        super();
    }

    public UncheckedException(String message)
    {
        super(message);
    }

    public UncheckedException(String message, Throwable cause)
    {
        super(message, cause);
    }

    public UncheckedException(Throwable cause)
    {
        super(cause);
    }
}

这篇关于Java 中异常的 throws 关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆