aspose file tools*
The moose likes Java in General and the fly likes how to catch Runtime exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to catch Runtime exception" Watch "how to catch Runtime exception" New topic
Author

how to catch Runtime exception

Ranganath Junpal
Ranch Hand

Joined: Aug 31, 2006
Posts: 76
hi all,

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

Thank you
Ranganath.s
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36467
    
  15
Welcome to the Ranch.

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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36467
    
  15
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.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
Ranganath Junpal
Ranch Hand

Joined: Aug 31, 2006
Posts: 76
well thanks guys.. i got my answer from both of you guys.. thanks a lot!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36467
    
  15
Glad we were able to help
Srinivas Kalvala
Ranch Hand

Joined: Oct 20, 2005
Posts: 257

Hello,

Write a simple thread which will wait to listen from the errStream of the Runtime.exec(), as soon as the data is present in the errStream , invoke another application with errStream as input.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to catch Runtime exception
 
Similar Threads
Open HTML file in desktop app
Exception redirect to page
redirecting output to a swing component
sql query
store a file in tomcat server