Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Catching uncaught exceptions

Started by Advanced Bug Feb 17, 2003 at 6:10 AM 3 replies 250+ views
Original Post
Advanced Bug
Advanced Bug
I am writing a very simple IDE for Java and I would want to be able to catch uncaught exceptions and report them (because Jave just prints the stack trace to console in such situations). The best thing of course would be catching exceptions in another Jave process (or Virtual Machine???), but if it''s impossible, catching exceptions from same thread would be fine. I have found the public void uncaughtException(Thread t, Throwable e) method in ThreadGroup class, but I have no idea what to do with it. Which leads to another question - is there a way to launch Java program from another Java program, other than calling Runtime.exec(), which doesn''t differentiate between Java and native processes?
CaptainJester
CaptainJester
quote:
Original post by Advanced Bug
I am writing a very simple IDE for Java and I would want to be able to catch uncaught exceptions and report them (because Jave just prints the stack trace to console in such situations). The best thing of course would be catching exceptions in another Jave process (or Virtual Machine???), but if it''s impossible, catching exceptions from same thread would be fine.


Yes. I am not sure about in a separate thread, but you can catch any exception in the same thread. Since unchecked exceptions propagate up the calling stack, you can catch them at the single point of entry and deal with them how you see fit. For example:

  
public class IDELauncher {
private dealWithIt(Exception e) {
//send messages to the user or whatever

}

public static void main(String args[]) {
IDE myIDE=new IDE();
while(true) {
try {
myIDE.go();
}
catch(Exception e) {
dealWithIt(e);
}
}
}
}


quote:
Original post by Advanced Bug
I have found the public void uncaughtException(Thread t, Throwable e) method in ThreadGroup class, but I have no idea what to do with it. Which leads to another question - is there a way to launch Java program from another Java program, other than calling Runtime.exec(), which doesn''t differentiate between Java and native processes?


Yes. You should be able to use Class reflection to launch another class.

If you look up ClassLoader.loadClass(String name), this returns a Class object. On this class object, you can call newInstance(). This will return a class that is an instace of the class you want, but will be returned as Object. You can then cast this to your class and call the appropriate methods, even static methods. For example:

  
String vars[]=new String[2];
vars[0]= "arg 1";
vars[1]= "arg 2";
ClassLoader cl = ClassLoader.getSystemClassLoader();
Class c = cl.loadClass("MyClass");
MyClass mine = (MyClass)c.newInstance();
mine.main(vars);

I have not tested this, but this is the way I pieced it together from the API docs.



First make it work,
then make it fast.

--Brian Kernighan

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
Advanced Bug
Advanced Bug
Thanks. This is exactly what I was trying to figure out.
Advanced Bug
Advanced Bug
Finally I managed to make work this launch Java program from another Java program thing. BTW, here''s the code:


  
URL u[] = new URL[1];
u[0] = new URL("file:d:/prog/java/tools/ide/dummy/");
URLClassLoader loader = new URLClassLoader(u);
Class c = loader.loadClass("Main");
Object o = c.newInstance();
Class a[] = new Class[1];
a[0] = String[].class;
Method m = c.getDeclaredMethod("main", a);
m.invoke(o, new Object[1]);

Advanced Bug
Advanced Bug
I have found even better solution. It turns out that Java SDK have built-in support for debugging in the com.sun.jdi package.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.