Asynchronous Exceptions, the third kind of Exceptions in Java

  

Consider the following code..

 try{
	while(true){
		System.out.println("test");
	}
}
catch(SQLException e){
	System.out.println(e);
}

This wouldn’t even compile because SQLException is a checked exception and it isn’t being thrown from anywhere in the try block.
However “Heinz M. Kabutz” claims that this can actually
be thrown from the try block!! Can you think how?

Here’s the trick.
Make this code execute in a thread t. Then get handle to t and stop this thread by calling


t.stop(new SQLException());


Even though the ’stop’ method is deprecated it is stil possible to use this method to throw arbitary exceptions(Even checked ones) . Before trying this, change the code to catch all exceptions instead of SQLException as otherwise, it wouldn’t even compile.

This kind of Exceptions, are called Asynchronous Exceptions! Cool. Thanks Dr. Heinz M. Kabutz.








Leave a Reply





Popular Articles

Blog Categories

Monthly Archives

Resources