0

我如何制作可以处理 IOException 的构造函数

这是带有构造函数的类

public class RootProcess {
Process process;

public RootProcess() throws IOException {

}

public RootProcess(Process process){
    this.process = process;
}

public Process getProcess() {
    return process;
}

public void setProcess(Process process) {
    this.process = process;
}


}

这就是我声明它的方式

RootProcess process = new RootProcess(Runtime.getRuntime().exec("su"));

但我在eclipse中得到这个错误

Default constructor cannot handle exception type IOException thrown by implicit super constructor. Must define an explicit constructor
4

1 回答 1

2

如果你从一个方法运行它,要么在方法签名中声明异常,要么使用 try/catch 块处理它。

声明异常:

public void someMethod() throws IOException {
     RootProcess process = new RootProcess(Runtime.getRuntime().exec("su"));
}

处理异常:

public void someMethod() {
try {
RootProcess process = new RootProcess(Runtime.getRuntime().exec("su"));
}
catch(IOException ex){
//handle it here
}
}
于 2013-01-24T11:57:26.323 回答