0

I've built a socket server which logs all it proccesses in a text file.

For now, the server runs in the background and has no UI.

Is there a way for me to catch ALL run time errors (not just the ones throwing exeptions) include null pointers etc. and log it to a file for monitoring?

6
  • 2
    What sort of run-time error doesn't throw an exception? Can you give a few examples? Commented Oct 28, 2012 at 6:46
  • You need to write the logging statements (according to your logger setting) to see what you want to see in your log file(s). Commented Oct 28, 2012 at 6:48
  • 1
    catch (RuntimeException re) catches all errors. Commented Oct 28, 2012 at 7:03
  • @Mankarse not all error make you use try/catch Commented Oct 28, 2012 at 7:31
  • You could try having a look at ThreadGroup#uncaughtException Commented Oct 28, 2012 at 7:44

1 Answer 1

2

Yes, it is simple to catch anything that can be thrown in Java. You just need to catch the base class of everything throwable:

try {
  ... my code ...
catch (Throwable t) {
  ... process it ...
}

A note on terminology: everything that can be thrown in Java is called an "exception", not only the exceptions extending from the Exception class. This is an unfortunate choice of class names, possibly due to a late decision in Java design to introduce a superclass to Exception.

Sign up to request clarification or add additional context in comments.

5 Comments

Late compared to what? On what evidence?
@EJP No evidence, it is my guess. If you have a better guess or evidence, please provide it for my education.
but then i have to wrap all my code and classes inside that right ?
I would hope you have a central place where you dispatch work from, like where you accept client requests. You need the catch only at that one place.
Some more advice in this respect: you must in fact ensure you don't catch any exceptions at other places in code. If checked exceptions give you trouble, write try { ... } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); }. This will ensure you let any unchecked exception through, but wrap any checked exception into RuntimeException.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.