pedja
pedja

Reputation: 3413

Constructor that handles exception

How can i make constructor that can handle IOException

this is the class with constructor

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


}

And this is how i declare it

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

but i get this error in eclipse

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

Upvotes: 0

Views: 158

Answers (1)

PermGenError
PermGenError

Reputation: 46398

if you running this from a method either declare the exception in method signature or handle it using try/catch block.

Declaring the exception:

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

Handle the Exception:

public void someMethod() {
try {
RootProcess process = new RootProcess(Runtime.getRuntime().exec("su"));
}
catch(IOException ex){
//handle it here
}
}

Upvotes: 2

Related Questions