Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching an exception from another running Java application

Tags:

java

exception

I've run into the issue where I have a program (not written by me, by someone else) I want to run 24/7, but sometimes it crashes. Normally, this wouldn't be an issue because I can simply create a process watcher that checks if it crashed, and then restarts it if necessary.

But, this particular program sometimes throws an exception and outputs it into the graphical interface that's integrated into it. In this instance, the program doesn't crash at all. The interface stays up, but the actual server functionality is unavailable.

Is there any way I can intercept this information from this process?

like image 674
Mike Bailey Avatar asked Sep 16 '11 14:09

Mike Bailey


People also ask

How do I catch an exception from another method?

If you want to enforce exception handling, you have to throw a subclass of Exception that is not derived from RuntimeException. But those exceptions have to be declared within the method Signature. Save this answer.

How can you catch an exception thrown by another thread in Java?

You need to pass it an object that implements the Thread. UncaughtExceptionHandler interface. This interface has only one method: uncaughtException(Thread t, Throwable e). This is the method that will be called on the passed object if an uncaught exception occurs in the run method."

Can you catch a Java runtime exception?

Runtime exceptions represent problems that are a direct result of a programming problem, and as such shouldn't be caught since it can't be reasonably expected to recover from them or handle them.

Is it good practice to Rethrow exception?

Catching and throwing exceptions is an overhead and is useless (except if you do something with it before re-throw, i.e. log it), so the programmer will actually be confused, thinking there is a bug and not understanding what the original intent was.


2 Answers

You want to use the Java Virtual Machine Tools Interface. I can't give you the code to catch your exception, but this is where to look. You'll have to do some detective work to find the class that throws the exception, or at least to find some indicator that it has been thrown.

Edit: You can also try calling the vendor to see if they know of a way. You can also look to see if it is writing the exception to a log file, which you could then watch.

like image 112
Jordan Bentley Avatar answered Nov 07 '22 07:11

Jordan Bentley


This may or may not work, but if when the application displays it's error and the server stops working does the memory usage drop? If so you could probably just add some logic to your process monitor to call the windows command tasklist to see if the memory usage drops below some threshold. You'll have to check how much memory the program normally uses and how much it uses after the error though.

Since you said the server functionality stops working, another option could be to write a simple program that basically just pings the server how ever often you want to make sure it is still up. If not, kill the process and restart it.

like image 38
Windle Avatar answered Nov 07 '22 07:11

Windle