Java声明“抛出异常”默认? [英] Does Java declare "throws Exception" by default?

查看:426
本文介绍了Java声明“抛出异常”默认?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下课程:

  class X 
{
public void met()
{
}
}

  class Y extends X 
{
public void met()throws NullPointerException
{
throw new NullPointerException() ;
}
}

从我读过的其他问题(为什么不能覆盖方法抛出比覆盖更广泛的异常方法? Java方法抛出异常



我明白子类中的重写方法必须抛出与覆盖方法中抛出的异常相同或子类。



Java方法总是至少引发异常类型的异常?



...换句话说,编译器会添加 throws异常



所以类X看起来像这样

  class X {
public void met()throws Exception {

}
}

我想澄清一点,我没有错误地说明:默认的异常处理程序异常总是被添加的事实。



相关问题: strong>



IOException与RuntimeException Java

解决方案

有两种类型的例外:检查异常(如 ParseException 解析文本时)和未选中的异常(例如 NullPointerException )。



检查异常必须在方法签名中声明。未标记的异常可能在方法签名中声明。



当覆盖方法(从接口或超级类)时,只需指定例外,您在执行中抛出。您不能声明在覆盖方法中不允许的实现中抛出被检查的异常。



允许这样做:

  class X {void someMethod()} 
class Y extends X {@Override void someMethod()throws UncheckedException}
/ pre>

这是不允许的:

  class X {void someMethod()} 
class Y extends X {@Override void someMethod()throws CheckedException}

这也是允许的:

  class X {void someMethod()throws CheckException} 
class Y extends X {@覆盖void someMethod()}


Consider following classes:

class X
{
public void met()
   {
   }
}

and

class Y extends X
{
    public void met() throws NullPointerException
    {
        throw new NullPointerException();
    }
}

From what I have read on other questions ( Why can't overriding methods throw exceptions broader than the overridden method? and Java method throwing exception )

I understand that the overriding method in the subclass, has to throw either the same or a subclass of the exception thrown in the overridden method.

Do Java methods always throw at least an exception of the type Exception?

...In other words the compiler adds throws Exception

So the class X would look like this

class X {
    public void met() throws Exception {

    }
}

I want to clarify that I am not mistaken about the fact that the default exception handler Exception is always added.

Related questions:

IOException vs RuntimeException Java

解决方案

There are two types of Exceptions: checked Exceptions (like ParseExceptionwhen parsing text) and unchecked Exceptions (like NullPointerException).

Checked Exceptions must be declared in the method signature. Unchecked Exceptions may be declared in the method signature.

When overriding methods (from an interface or a super class) you only have to specify the exceptions, that you are throwing in your implementation. You cannot declare to throw checked exceptions in an implementation that are not allowed in the overridden method.

This is allowed:

class X { void someMethod() }
class Y extends X { @Override void someMethod() throws UncheckedException }

This is not allowed:

class X { void someMethod() }
class Y extends X { @Override void someMethod() throws CheckedException }

This is also allowed:

class X { void someMethod() throws CheckException }
class Y extends X { @Override void someMethod() }

这篇关于Java声明“抛出异常”默认?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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