问题
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();
}
...
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
?
回答1:
Exception Handling ways:
We can handle a 'problem statement' (Problem Statement is any statement which can throw an exception) in two ways.
- Enclose the statement in
try-catch
block. - Append a
throws
clause in the method header.
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.
Examples:
Enclose the statement in try-catch
block.
public void myMethod(){
try{
// Problem Statement
}catch(IllegalAccessException e){
e.printStackTrace();
}catch(NullPointerException e){
e.printStackTrace();
}
}
Append a throws
clause in the method header.
public void myMethod()
throws IllegalAccessException, NullPointerException{
// Problem Statement
}
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();
}
}
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();
}
}
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();
}
}
}
回答2:
In Java you have to either catch exceptions or declare the fact that calling the method might result in an exception, like so:
public void onSomethingOtherThanCreate(Bundle savedInstanceState)
throws IOException, XmlPullParserException
{
// your code without any try/catch
}
(Error
and RuntimeException
exceptions which can happen any time do not need to be declared in this way.)
回答3:
It's not Eclipse, it's Java!
In a method, code which throws exceptions must reside inside a try-catch
block unless the throws
clause for the unhanded exception is used in the method declaration itself.
回答4:
That's what checked exceptions mean: you must either catch them, or declare them thrown.
onCreate
is being overridden and must adhere to the original method's declaration, so either catching them, or doing that part of the work in another method that catches them, is your only option.
See JLS 11 Exceptions, specifically JLS 11.2.3 Exception Checking, for details.
回答5:
If your complaint is that you have to type two catch clauses, it is possible to catch them all in a single clause,
catch (Exception e) {
// dostuff here
}
However, unless all you plan to do is print a stack trace or log the exception, this practice is frowned upon. In the long run it will make the code harder to debug and maintain.
Java 7 allows you to catch multiple exceptions in a single clause, but Android doesn't yet support Java 7.
来源:https://stackoverflow.com/questions/9045990/do-i-have-to-include-catch-blocks-in-java-when-calling-a-method-that-throws-exce