![]() |
Exception Handling with Threads
I am writing a client application in Java. It can open connections to a few different servers at once. Each connection has it's own Thread. I thought to myself, how should I handle exceptions? I would like to hear other's thoughts before I share what I am currently doing.
Thanks. |
There is no single correct answer to your question. The way you handle exceptions depends on what exceptions are thrown, the reasons they are thrown, what needs to be done to recover when they are, how the threads that may encounter errors interact with each other, etc etc.
|
The server code
:
import java.util.*;:
import java.net.*;Exception catching is a must when dealing with the io and net packages. Read up the Java Doc for JSE 1.5 and see what methods throw what exception. Once you get the hang of it, it's very very easy. I did this a long time ago, Networking in Java is really fun! |
P.S. - This should be moved to the Java Section.
|
I disagree. It's a design issue that happens to be implemented in Java, but the issues are more widely applicable. A person seeking solutions in this area is more likely to look here than in a forum for a specific language. It's why this forum was recently added.
|
Aervos and I recently had a discussion about this in the VB.NET forum. We got into it pretty heavily, and there's a few good links in the discussion.
Linky: http://www.programmingforums.org/for...ight=Exception As a summary, some like to use more exceptions, others less, there's good reasons for each method. It's up to you to decide what's appropriate for your project. -MBirchmeier |
Indeed. Exceptions are one type of error handling mechanism. One way of handling exceptions is not to throw them at all: in other words, managing error conditions in another way. Error handling philosophies are basic design issues for any program.
For some discussion of exceptions, with examples in C++, but the same basic design issues, look at a discussion by Herb Sutter or this discussion by David Abrams or this discussion by David Abrams on exception safety or a powerpoint presentation by Andree Alexandrescu on use of exceptions versus return codes Little specifically to do with threads, but if you can't get it working with one thread you haven't a hope with multithreading. |
Though some method throw exception. It's a must in some cases that exceptions have to be caught. Take the example I wrote for you in mind, that exceptions handling was a must. In Java, some method and constructor throw exception.
With that said, if you don't have to use exceptions, don't use them. There are many other alternatives to exceptions where they are not required. Exception handling in Java slows down performance of a program. Where you use a "throw/catch" statement, try to use an "if" statement instead. |
Thanks for all of the input. I didn't realize I could catch errors from a running Thread the same way that you normally would.
The method I chose is more in line with your last post, reggaeton_king. Any Exception that happens in the Threaded connection to the server will be fatal. The Server sends an exit message when it wants a client to exit. I simply created an exit message and threw it into the client's message queue so it looks like it came from the server. The client decodes it normally and hands it just as if the server had sent it, although it can tell: A) that it came from the client. B) which exception caused it. I am glad you posted that code, because I am positive that will help me in the near future. :) |
Quote:
|
| All times are GMT -5. The time now is 12:21 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC