www.digitalmars.com         C & C++   DMDScript  

D.gnu - Exception can't work in multithreading

reply King Dark <gdxxhg gmail.com> writes:
Hi, I am recently writing a web server and it needs multi-threading. But I met
a problem that the exception can not work after I create a thread.

This is the code without multi-threading, it works well.

				try{
					printf("exception1");
					throw new Exception("hello");
				}catch( Exception e ){
					printf("exception3");
				}
It printed "exception1" and "exception3" in console.

This is the code with multi-threading:
				server.start(); //creates a listening thread for socket.
				try{
					printf("exception1");
					throw new Exception("hello");
				}catch( Exception e ){
					printf("exception3");
				}
It printed "Error: Hello" in console.

I don't know why. I think my code of multi-threading is correct( as I am using
std.thread ). Have any one ever met the same problem?

By the way, the "try catch" seems cannot catch the exception such as "divided
by zero". It crushed after running such code.
Mar 17 2008
parent "Scott S. McCoy" <tag cpan.org> writes:
Content-Type: text/plain
Content-Transfer-Encoding: 7bit


On Mon, 2008-03-17 at 10:29 -0400, King Dark wrote:

 
 By the way, the "try catch" seems cannot catch the exception such as
 "divided by zero". It crushed after running such code.

Although your aforementioned problem appears relatively legitimate, attempting to catch an exception resulting from division by zero is a terribly bad practice. Try testing to see if the input value (the untrusted value) is non-zero first instead. This is what reasonable programs do (kind of like testing for an object being null before calling a method on it, rather than trying to catch and handle the exception which results from calling a method on a null value). Cheers, Scott S. McCoy
Mar 23 2008