Asaf Nevo
Asaf Nevo

Reputation: 11678

Java how to catch ALL run time errors

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?

Upvotes: 0

Views: 4516

Answers (1)

Marko Topolnik
Marko Topolnik

Reputation: 200158

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.

Upvotes: 2

Related Questions