Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch exceptions within an entire Java application [duplicate]

Is there any method to handle/catch all exceptions thrown by a Java application, without spamming costly try{}catch(Exception e){} statements everywhere?

For example, PHP has the function set_error_handler().

Back story - Despite tough testing, sometimes bugs can get through, and users are always less than cooperative with helping to fix these. Ideally, I'd like to hook the application up to a web service facility that can keep track of any stack traces thrown by a user's application.

like image 249
Ben Poulson Avatar asked Mar 17 '14 17:03

Ben Poulson


People also ask

Can a java program catch multiple exceptions?

In Java SE 7 and later, we can now catch more than one type of exception in a single catch block. Each exception type that can be handled by the catch block is separated using a vertical bar or pipe | .

Can an exception be caught twice?

Java does not allow us to catch the exception twice, so we got compile-rime error at //1 .

How do you catch multiple exceptions with a program?

Java allows you to catch multiple type exceptions in a single catch block. It was introduced in Java 7 and helps to optimize code. You can use vertical bar (|) to separate multiple exceptions in catch block. An old, prior to Java 7 approach to handle multiple exceptions.

Is there a way to catch multiple exceptions at once and without code duplication in C#?

In C#, You can use more than one catch block with the try block. Generally, multiple catch block is used to handle different types of exceptions means each catch block is used to handle different type of exception.


1 Answers

Perhaps you're looking for Thread.setDefaultUncaughtExceptionHandler() ?

like image 152
Eric Stein Avatar answered Oct 12 '22 05:10

Eric Stein