The Question is how to catch an exception and redirect it to show in say a notepad or some thing like that.. to be clear.. say i have a swing with 2 panes and Jtextbox.. once i enter ls in textbox it gonna invoke runtime.getruntime.exec(); method to execute.. if the output is successful i wanna know the result at the one pane. and if there is any exception i wanna show it in another pane.. how to redirect the standard output .. expecting a speedy response
Your question is a little confusing; all Exceptions occur at runtime, but there is a class called RunTimeException (I might be mistaken about the capital T), which is an "unchecked exception," as are all its subclasses. Most instances of RunTimeException represent errors in the coding somewhere (eg NullPointer, IllegalArgument SomethingOutOfBounds, Arithmetic Exceptions). So if you get any of them, you need to go back to the coding and find out where it happened and correct something. I have tried converting an unchecked Exception to checked, and never managed it. I think it is impossible.
If it an Exception which is not a subclass of RunTimeException, then it will be checked (IOException is probably the commonest example) and your coding must have some sort of Exception handling or it won't compile. It would be quite easy for you to put some sort of coding in the catch block to write to a text file. Use the old-fashioned method of writing using a FileWriter rather than a Scanner (because you can put "true" in a FileWriter constructor to get it to append text), and write your stack traces to a text file. You can write an ExceptionWriter class, and still get it to show the details on screen using "System.err.print(myException);".
If you require more details, use logging, but I can't remember how that works.
Joined: Oct 13, 2005
I don't know whether my previous answer helped, but it is just as easy to direct the message from a catch block to a different part of your GUI as to a text file.
I'm reading the OP differently - trying to capture exception messages from the process started by Runtime.exec().
You can capture the stdout and errout streams of the started process. You actually have to do this to make some processes run correctly so it's a good idea all the time. This article shows how to make "stream gobbler" threads. You could make those update your Swing panels.
Was that the right question?
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Aug 31, 2006
well thanks guys.. i got my answer from both of you guys.. thanks a lot!