-1
class simpleShell{
    Process sh;
    simpleShell(String shell)  {
        try {
            sh = Runtime.getRuntime().exec(shell);
        }catch (Exception e){
            throw e;
        }
    }

}
class Main{
public satic void main(String args[]){

try {
    simpleShell ss = new simpleShell("sh");
}catch{
    //do something
}

I am trying to make a shell class for an app. But the IDE keeps reporting "IOException" not handled and suggests me to use throws. I want to handle exceptions when function called not ignore them. I though constructors cant throw but some says otherwise. I even tried doing a separate method to create the process and throw the exception and tried throwing other exceptions. But same report.

The report is at throw statement.

Riyadh Kabir
  • 19
  • 1
  • 4
  • You're throwing an exception from the method but it does not declare it as such. You need to add `throws Exception` to the signature or __not__ use the `throw e` inside the method. – rdas Mar 02 '21 at 04:14
  • First: Java Class names start with a capital letter. Second: Of course can constructors throw Exceptions. – Benjamin M Mar 02 '21 at 04:15
  • 1
    What does this snippet of code have to do with constructors throwing exceptions? Are you sure this code is representative of what you're asking about? – Sotirios Delimanolis Mar 02 '21 at 04:18
  • Sorry for poor question quality.But its not similar to those posted. Can u see the edit and help me again. – Riyadh Kabir Mar 02 '21 at 05:22

1 Answers1

0

A method (or constructor) which calls a method which has throws IOException (or any other type of exception of course) needs to either catch the exception, or declare that it throws it too.

When a method declares that it throws an exception, then its callers must also either catch or declare the exception.

So you can either:

void create(String shell){
        try {
            sh = Runtime.getRuntime().exec(shell);
        }catch (IOException e){
            // handle the exception in some way
        }
}

or

void create(String shell) throws IOException {
   sh = Runtime.getRuntime().exec(shell);
}

If you want to catch the exception, do something, and then rethrow it you need to declare that your method throws the exception:

void create(String shell) throws IOException {
        try {
            sh = Runtime.getRuntime().exec(shell);
        }catch (IOException e){
            // handle the exception in some way
            // then rethrow it
            throw e;
        }
}
tgdavies
  • 10,307
  • 4
  • 35
  • 40
  • Parhaps my qeustion was not clear. I want to catch the exception which is ok. But i want to throw the the catched exception to the caller Function. When ever i try to thow it, ide reports IOException not handled – Riyadh Kabir Mar 02 '21 at 05:14
  • @RiyadhKabir I've added an example showing that behaviour – tgdavies Mar 02 '21 at 05:21
  • But doent throws supress the exception?What if the exec sends IOExcption and i cant catch it cause of throws – Riyadh Kabir Mar 02 '21 at 05:28
  • No, `throws` forces callers of the function to either catch or declare the exception. – tgdavies Mar 02 '21 at 05:38