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

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

问题描述

当你这样做:

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);
    }
}

UncheckedException.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);
    }
}

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

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