-6

I'm coding, to open windows calculator, but the compiler is giving an error:

unreported exception IOException; must be caught or declared to be thrown.

If I write throws against the ActionListener method then it is not overridden.

Part of the code:

else if(e.getActionCommand().equals("Window Calculator")) {
    Runtime.getRuntime().exec("calc");   
}
Jamie Taylor
  • 4,709
  • 5
  • 44
  • 66
Harsh
  • 13
  • 1
  • 4
  • You need to catch the exception and log the error to the screen or at least log that the operation failed, or at the very least ignore it and pretend it doesn't matter. – Peter Lawrey Feb 14 '14 at 11:40
  • You are getting "*unreported exception IOException; must be caught or declared to be thrown*" because there is an unreported exception IOException that must be caught or declared to be thrown :) – Maroun Feb 14 '14 at 11:41
  • If i catch the exception,how will it help..the code that i want to use will not be executed by this..:( – Harsh Feb 14 '14 at 11:45
  • @Harsh Exceptions happens because something you don't expect happens. So you should know how to handle them. – Maroun Feb 14 '14 at 12:09
  • brother..i know that..but i need help..i want to know that is there any other way to call window's calculator with code..? is there any substitute for my code.." Runtime.getRuntime().exec("calc");".??? – Harsh Feb 14 '14 at 12:11
  • @Harsh Your code is fine, just surround that with `try-catch`. The line **will be executed**. – Maroun Feb 14 '14 at 12:48
  • @MarounMaroun ..sir,by this only the catch part is executing not the try..because exception is been thrown by the code i have used...i.e " Runtime.getRuntime().exec("calc");" – Harsh Feb 14 '14 at 13:09

4 Answers4

2

You need to handle the exception for Runtime.getRuntime().exec("calc");.

Try this:

else if(e.getActionCommand().equals("Window Calculator")){
    try
    {
        Runtime.getRuntime().exec("calc");
    }
    catch(IOException ioe)
    {
        ioe.printStackTrace();
    }
}

From OP comments:

If i catch the exception,how will it help..the code that i want to use will not be executed by this

This will not help you to run your code which has caused exception, but it can help you to run your other parts of program even if exception occurs.

Bhushan
  • 6,151
  • 13
  • 58
  • 91
1

You need to handle checked exceptions at compile time or throw them.However,if you throw new or broader checked exceptions than the super method or interface method,then it violates overriden or interface implementation rules.So you have the only option of handling them in your code.

Note:- In your case ,you can either handle your checked exceptions and swallow them or wrap it around an unchecked exception(Runtime Exception) and throw the Runtime Exception.You can always throw a new unchecked(runtime exception)compared to your superclass or interface method

Kumar Abhinav
  • 6,565
  • 2
  • 24
  • 35
  • if a write throws IOException then the method gives an compiler error that the overriden method doesn't throws exception...and if i use try/catch then that exception will be handled but my code will not execute...i want my line to be execute..but not with the exception..is there any other way to open my windows calc.? – Harsh Feb 14 '14 at 12:00
  • @Harsh Please explain this sentence.I am unable to get through "And if i use try/catch then that exception will be handled but my code will not execute" – Kumar Abhinav Feb 14 '14 at 12:02
  • if the write my code in try..and catch the exception in catch..code written in catch will execute but,i want to execute the code written in try block..i.e " Runtime.getRuntime().exec("calc");". is there any other way to call windows calc with code..? – Harsh Feb 14 '14 at 12:08
1

You cant add throws declaration to an overriden method, since this is changing the method's signature. Use try {} catch {} instead.

dstronczak
  • 2,406
  • 4
  • 28
  • 41
  • if i use try/catch then that exception will be handled but my code will not execute...i want my line to be execute..but not with the exception..is there any other way to open my windows calc.? – Harsh Feb 14 '14 at 11:59
0

use try/catch block to handle the checked exception locally. Look at Jenkov's Tutorial for a good short tutorial on the subject.

To launch an external program from Java, you can use ProcessBuilder. The Runtime.getRuntime().exec() internally does the same thing. More discussion on this in this thread

Community
  • 1
  • 1
Ankit Kumar
  • 1,433
  • 1
  • 16
  • 24
  • if i use try/catch then that exception will be handled but my code will not execute...i want my line to be execute..but not with the exception..is there any other way to open my windows calc.? – Harsh Feb 14 '14 at 11:54