Let a runtime exception go beyond a Thread?

J

Jaap de Bergen

Hello all,

I'm using a O/R framework which throws a runtimeexception when
something goes wrong (for example a column is missig from the
database).

A couple of facts:
I've made a couple of DAO's which access the O/R framework.
The DAO's are being accessed by a Thread.
I'm starting my application with the following code:
App a=new App();

I try to catch the runtimeexceptions which the O/R framework throws at
the root of my program:
try{
App a=new App();
}
catch(Runtimeexception e){
displayDialog(e)'
}

That was a good idea, but it doesn't work. I guess because when i
launch a thread:

MyThreadt=new MyThread();
t.start()

MyThread extends Thread{
public void run(){
DAO.accesDAOfunction();
}
}

and call a DAO function in that thread the runtimeexception will not
go further "upwards" then MyThread. In other words it will never
reach:

try{
App a=new App();
}
catch(Runtimeexception e){
displayDialog(e)'
}

Does anybody know how i can get the runtimeexception reach the root of
my application?

Kind regards,


Jaap
 
J

Jaap de Bergen

try{
App a=new App();
}
catch(Runtimeexception e){
displayDialog(e)'
}

Does anybody know how i can get the runtimeexception reach the root of
my application?

I've wrote a little example to illustrate the problem:
===========
import java.awt.*;

public class ExceptTest {
public static void main(String[] args) {
try{
ExceptTest et = new ExceptTest();
}
catch(RuntimeException e){
System.out.println("runtime exception: "+e.getMessage());
}
}

public ExceptTest() {
new Worker().work();
}

class Worker{
void work(){
MyThread mt=new MyThread();
mt.start();
}
}

class MyThread extends Thread{
public void run(){
DoSomething o=new DoSomething();
o.doIt();
System.out.println("Ready");
}
}

class DoSomething{
void doIt(){
throw new RuntimeException("boooe!");
}
}
}
===========

The doIt() function of DoSomething throws a RuntimeException, but this
RuntimeException never reaches the "catch(RuntimeException e)" part of
the main(String[] args) function.

Is there a way to make the RuntimeException get to the main(String[]
args) function?



Jaap
 
G

Gordon Beaton

I'm using a O/R framework which throws a runtimeexception when
something goes wrong (for example a column is missig from the
database).
[...]

That was a good idea, but it doesn't work. I guess because when i
launch a thread:

That's because each thread has its own call stack, and exceptions only
propagate up the call stack.
Does anybody know how i can get the runtimeexception reach the root
of my application?

There is a way you can be notified of exceptions occurring in other
threads. Have a look at ThreadGroup.uncaughtException(), or (if you're
using 1.5) Thread.setUncaughtExceptionHandler().

/gordon
 
J

Jaap de Bergen

I'm using a O/R framework which throws a runtimeexception when
something goes wrong (for example a column is missig from the
database).
[...]

That was a good idea, but it doesn't work. I guess because when i
launch a thread:

That's because each thread has its own call stack, and exceptions only
propagate up the call stack.
Does anybody know how i can get the runtimeexception reach the root
of my application?

There is a way you can be notified of exceptions occurring in other
threads. Have a look at ThreadGroup.uncaughtException(), or (if you're
using 1.5) Thread.setUncaughtExceptionHandler().

Thanks Gordon. The ThreadGroup solution works perfect.

As a side note/joke: i'm wondering where a exception will end when i
throw it in ThreadGroup.uncaughtException(), it seems to disappear
completely ;-)


Jaap
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top