Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to catch Runtime exception

 
Ranganath Junpal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48646
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48646
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ranganath Junpal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well thanks guys.. i got my answer from both of you guys.. thanks a lot!
 
Campbell Ritchie
Sheriff
Posts: 48646
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad we were able to help
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic