Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

threads and exception handling  RSS feed

 
saran sadaiyappan
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Assume I have a piece of code like
try {
some process handled by a Thread1
}catch(){
e.printstacktrace();
}
I want to know whether the stack is printed by Thread1 or a seperate thread
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not compilable code, in loads of ways. However, I think I see what you are asking.

The printing of the stack trace nominally takes place on the same thread. It is a JVM implementation detail whether printing to standard output (or standard error) actually involves any other threads.
 
saran sadaiyappan
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peter. Yeah..thats not a compatible code but I just put the essence of my question in that. So by your answer my understanding is that, if I print it to a stdOut or stderr then a new thread will be started.Rite?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Er, no, that's not what I said. From the Java programs's point of view, console (stdout or stderr) output occurs on the current thread. Maybe the JVM's implementation might involve other thread(s), but that would be a JVM implementation detail, which should not normally concern us.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wonder if your code was meant to be:

If MyRunnable throws an exception in its constructor, your catch block will get it. But if it throws an exception in its run() method, this code cannot possibly catch it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!