我有打电话抛出异常的方法时,包括在Java的catch块? [英] Do I have to include catch blocks in Java when calling a method that throws exceptions?

查看:348
本文介绍了我有打电话抛出异常的方法时,包括在Java的catch块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个Android应用程序。和类型化约codeS就像:

i create an android application. and typed some codes just like :

public void onCreate(Bundle savedInstanceState) {
...
XmlResourceParser xrp = getResources().getXml(R.xml.OneXml);   <==OneXml is a custom xml file in folder res/xml
...
try {
    ....
    xrp.next();
    .... 
} catch(IOException e) {
        e.printStackTrace();
} catch(XmlPullParserException e) {
        e.printStackTrace();
} 
...

我发现我既不能删除赶上(IOException异常E)也不赶上(XmlPullParserException E),当我尝试删除其中的一个,在Eclipse的标记错误xrp.next()并告诉我, 未处理的异常类型为IOException 未处理的异常类型XmlPullParserException ,所以我必须添加两个?为什么Eclipse的问我补充两个强制?不能在code被无两捕捉成功编译

i found that i can neither delete catch(IOException e) nor catch(XmlPullParserException e), when i try to delete one of them, Eclipse marks an error at xrp.next() and tells me that Unhandled exception type IOException or Unhandled exception type XmlPullParserException, so must i add the two catch? why does Eclipse ask me to add two catch compulsively? Can't the code be compiled successfully without the two catch?

推荐答案

异常处理的方式:

我们可以处理一个'问题的声明(问题陈述是可以抛出一个异常的陈述)两种方式。

Exception Handling ways:
We can handle a 'problem statement' (Problem Statement is any statement which can throw an exception) in two ways.


  • 括在语句的try-catch 块。

  • 在方法头附加一个抛出子句。

  • Enclose the statement in try-catch block.
  • Append a throws clause in the method header.

在重载方法的处理异常:

如果您正在覆盖在子类中的方法。然后,你不能追加额外的在其签名throws子句。在你的情况的onCreate 是父类,这是在子类中重写因此我们不能追加的方法抛出这头条款。所以,你必须附上内的onCreate任何'问题陈述方法的try-catch块。

Handling Exception in Overridden Methods:
If you are overriding a method in child class. Then you cannot append extra throws clause in its signature. In your case onCreate is a method in parent class, which is overridden in child class hence we can not append throws clause in it header. So you have to enclose any 'Problem Statements' within onCreate method in try-catch blocks.

例子:

括在的try-catch 块中的语句。

public void myMethod(){
        try{
            // Problem Statement
        }catch(IllegalAccessException e){
            e.printStackTrace();
        }catch(NullPointerException e){
            e.printStackTrace();
        }
    }

在方法头

追加一个抛出子句。

public void myMethod()
        throws IllegalAccessException, NullPointerException{
            // Problem Statement
    }

例子覆盖的方法:

有效throws子句::一种抛出重载方法条款是有效的,如果它也是parent'smethod

Examples Overridden Methods:
Valid throws clause: A throws clause in overridden method is valid if it is also in parent'smethod.

public class Parent {
    public int method() 
        throws IllegalAccessException,NullPointerException, Exception {
        return 0;
    }
}
public class child extends Parent {
    // throws is part of method because parent method
    // have throws clause
    @Override
    public int method() 
        throws IllegalAccessException, NullPointerException,
            Exception {
            //Problem Statement
        return super.method();
    }
}

无效throws子句::一种抛出重载方法条款无效,如果它不是在父类的方法present

Invalid throws clause: A throws clause in overridden method is invalid if it is not present in parent's method.

public class Parent {
    public int method(){
        return 0;
    }
}

public class child extends Parent {
    //**Error:** We can not append throws clause in the method because
    // parent method do not have a throws clause
    @Override
    public int method() 
        throws IllegalAccessException, NullPointerException,
            Exception {
            //Problem Statement
        return super.method();
    }
}

所以,我们要修改我们的子类的方法,并删除抛出子句作为父类的方法不包含在这种特殊情况throws子句。我们必须处理通过异常的try-catch 块。

So we have to modify our child class's method and remove throws clause as parent method do not contain throws clause in this particular situation. We have to handle exception through try-catch block.

public class child extends Parent {
    //**Error:** We can not append throws clause in the method because
    // parent method do not have a throws clause
    @Override
    public int method() {
        try{
            // Problem Statement
        }catch(IllegalAccessException e){
            e.printStackTrace();
        }catch(NullPointerException e){
            e.printStackTrace();
        }
    }
}

这篇关于我有打电话抛出异常的方法时,包括在Java的catch块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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