3 Replies - 262 Views - Last Post: 24 July 2013 - 07:51 PM Rate Topic: -----

#1 ray1234  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 07-September 12

Thread exception question

Posted 24 July 2013 - 07:27 AM

I'm new to threading in a multithreaded environment so please bear with me..

I created a very simple console program where i wanted to creates 4 threads, and throws a unhandled exception for the first thread and the rest of threads to run without errors. Here, the whole application terminates on the first thread, without Main completing the other threads.

So from this i learnt that an unhandeled exception on one thread cause the whole application to terminate. I was actually hoping that only the thread that caused the exception would terminate, and letting main complete its work.

So in a multithreaded app, if User A use the app to retrive some data, User A operation would be affected by an error from another user's operation on that app.

I know try,catch statements can prevent this, but from reading i learnt it isn't a good practice to add these statements all over my code for the sake that an error may arrise without handling it properly. You don't know the error specifically, and can't handle the exception efficiently. but generally i like my app to throw exceptions to be thrown so that errors can be logged

so my question would be is there a way where an exception in one thread cannot terminate the whole application

i tried googling but no luck

Is This A Good Question/Topic? 0
  • +

Replies To: Thread exception question

#2 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4531
  • View blog
  • Posts: 7,902
  • Joined: 08-June 10

Re: Thread exception question

Posted 24 July 2013 - 08:12 AM

Quote

but from reading i learnt it isn't a good practice to add these statements all over my code for the sake that an error may arrise without handling it properly. You don't know the error specifically, and can't handle the exception efficiently. but generally i like my app to throw exceptions to be thrown so that errors can be logged


Well...there's some things wrong with this statement. It's good to catch errors. Research the potential errors your code can create, and plan ahead for them. Capture them and handle them properly. This is one area that Java has a really cool feature: checked exceptions. But the point is, you should strive to not have any unhandled exceptions in your program at all. Use proper logging, but don't use unhandled exceptions as a means of program flow control.
Was This Post Helpful? 1
  • +
  • -

#3 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Thread exception question

Posted 24 July 2013 - 09:13 AM

View Postray1234, on 24 July 2013 - 07:27 AM, said:

So in a multithreaded app, if User A use the app to retrive some data, User A operation would be affected by an error from another user's operation on that app.


If two users are accessing the same running software (like a server) then yes. If they each run their own copy, then no.

And if you are creating a server, you better be handling exceptions in threads.
Was This Post Helpful? 1
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3621
  • View blog
  • Posts: 11,278
  • Joined: 05-May 12

Re: Thread exception question

Posted 24 July 2013 - 07:51 PM

I've not tried it, but I believe that if you set up multiple app domains then you could let the appdomains die when an unexpected exception happens. Unfortunately, I feel that the overhead of setting up and maintaining multiple app domains is dramatically higher than just setting up multiple threads and simply catching the exception and figuring out if the exception is something that is recoverable, or something that should be considered fatal.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1