akbertram
Posts: 1
Joined: Sat Sep 18, 2021 8:49 am

Best practices for Java Service Error Handling

Sat Sep 18, 2021 8:58 am

Hi there,
I have followed the tutorial for building a Java Service installer and have managed to build an installer for our application. The documentation is very clear and this is exactly what we need.

There was one question I couldn't find an answer to: how should my Java application handle errors during start or stopping? Should I throw an exception or call System.exit()? Or is there some other way to signal that the service cannot start?

While I was working on my installer, I ran into a few bugs on my side that led to an exception being thrown from main(), and then from stop().

The exception from main() seems to have been ignored, and the service reported starting.

The exception from stop() led to the service hanging, and could only be stopped by terminating the process.

While stopping, I will just be sure to catch and log all exceptions and return.

But how can I signal from Java that I have encountered a fatal error and cannot start the service?

Thanks!

Alex

Catalin
Posts: 4428
Joined: Wed Jun 13, 2018 7:49 am

Re: Best practices for Java Service Error Handling

Mon Sep 20, 2021 3:22 pm

Hello Alex and welcome to Advanced Installer forums,

Unfortunately, I am no expert in Java so I can't really do much to assist in this case.
There was one question I couldn't find an answer to: how should my Java application handle errors during start or stopping? Should I throw an exception or call System.exit()? Or is there some other way to signal that the service cannot start?
I think the best approach would be throwing an exception, because that might help you and the user find out why the service actually failed (i.e. not enough privileges), rather than simply exiting the process.

Hope this helps somehow!

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Building Installers”