So I want to intercept the unhandled exceptions and I am doing this by:
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
#Override
public void uncaughtException(Thread t, Throwable e) {
//MY CUSTOM CODE TO SEND LOGS
System.exit(2);
}
});
Is there anyway I can execute my custom code and then resume normal way it handles these exceptions? I believe I am getting some hanging issues since System.exit doesn't seem to be working reliably.
You have to get the default handler first. Then intercept and send the error too the handler. System.exit... is a bad idea.
So first you create a custom exceptionhandler like you did:
public class CustomExceptionHandler implements Thread.UncaughtExceptionHandler {
private final Thread.UncaughtExceptionHandler defaultUEH;
public CustomExceptionHandler(Thread.UncaughtExceptionHandler defaultUEH) {
this.defaultUEH = defaultUEH;
}
#Override
public void uncaughtException(Thread thread, Throwable ex) {
String stackTrace = Log.getStackTraceString(ex);
String time = new Date(System.currentTimeMillis()).toString();
String message = ex.getMessage();
// send logs...
defaultUEH.uncaughtException(thread, ex);
}
}
So basically in your onCreate method (best used in a custom Application class to have it fall all parts) set the default exception handler if not already set (check not needed in Application):
Thread.UncaughtExceptionHandler defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
if (!(defaultUEH instanceof CustomExceptionHandler)) {
Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler(defaultUEH));
}
I wrote it here (remove the BuildConfig.DEBUG check) implemented to directly start the email client. If there is no email client installed, you should catch the ActivityNotFoundException.
Related
I have a FileCreator class that implements StreamResourceWriter interface and MainErrorHandler class that implements ErrorHandler. I'm using the MainErrorHandler class as a centralized Exception handler in my project which mostly logs the exception and shows a notification to the user. The problem is that StreamResourceWriter.accept() method runs in a non UI thread and when an Exception is thrown it is directed to the ErrorHandler which then fails to show a notification due to "IllegalStateException: UI instance is not available". Is there a way to show a notification window to the user from MainErrorHandler when FileCreator throws an error in accept() method?
Below FileCreator snippet.
public class FileCreator implements StreamResourceWriter {
#Override
public void accept(OutputStream stream, VaadinSession session) throws IOException {
// Run in a non ui thread.
// Writes to OutputStream but an Exception might be thrown during this process
}
}
Below MainErrorHandler snippet.
/**
* Centralized error handler
*/
public class MainErrorHandler implements ErrorHandler {
private static final Logger log = LoggerFactory.getLogger(MainErrorHandler.class);
#Override
public void error(ErrorEvent event) {
log.error("Error occurred", event.getThrowable());
//Cannot show a notification if ErrorEvent came from FileCreator.
//Will get an IllegalStateException: UI instance is not available.
Notification.show("Error occurred");
//Tried UI.getCurrent but it returns null if ErrorEvent came from FileCreator.
UI.getCurrent();
}
}
Using Vaadin 13.0.1.
Edit
One way to solve this issue is to pass UI reference to FileCreator directly. Below an example.
public class FileCreator implements StreamResourceWriter {
private UI ui;
//Pass UI reference directly
public FileCreator(UI ui){
this.ui = ui;
}
#Override
public void accept(OutputStream stream, VaadinSession session) throws IOException {
try{
// Run in a non ui thread.
// Writes to OutputStream but an Exception might be thrown during this process
}catch(Exception e){
//I don't like this since have to catch all exceptions and have to call ErrorHandeler directly with a UI reference. Also what if somewhere in code ErrorHandler is changed and is not of type MainErrorHandler.
((MainErrorHandler)VaadinSession.getCurrent().getErrorHandler()).error(e, ui);
}
}
}
As I said in comments I really don't like this approach either since I am forced to catch all Exceptions, have to cast ErrorHandler to MainErrorHandler and calling it directly.
There is a way, but it's not perfect.
You can get all UI instances via VaadinSession.getCurrent().getUIs().
To filter out the inactive/detached UIs you can check if ui.getSession() returns a VaadinSession (so, not null). The JavaDoc of getSession says:
The method will return null if the UI is not currently attached to a VaadinSession.
Then you can invoke the access method on each of the UIs and create and show the notification inside the UI-context.
for(UI ui : VaadinSession.getCurrent().getUIs()) {
// Filtering out detached/inactive UIs
if (ui.getSession() != null) {
ui.access(() -> {
// create Notification here
});
}
I said it's not perfect because you have to keep in mind that the user can have several UIs opened at the same time(e.g. multiple tabs).
I have a common project with some shared code that is being used in another project. I'm trying to convert/map the exception from the common project CommonException to a new type of Exception let's call it SuperAwesomeException.
The aim is to have a generic way of handling all custom exceptions in the project.
I've attempted to do this using an UncaughtExceptionHandler. This seems to work when running the project but not from within JUnit, since that wraps each test in a try/catch block as described here.
public final class ExceptionHandler implements Thread.UncaughtExceptionHandler {
#Override
public void uncaughtException(Thread thread, Throwable exception) {
if (exception instanceof CommonException) {
throw new SuperAwesomeException(exception.getMessage());
}
if (exception instanceof SuperAwesomeException) {
throw new CommonException(exception.getMessage());
}
else {
System.out.println("ERROR! caught some other exception I don't really care about");
System.out.println("Not doing anything");
}
}
}
Is there another way I can map from one Exception to another or can I somehow tell JUnit not to catch certain exceptions and check the Exception is mapped to the correct one?
UPDATE - How I initially tried to write the Test:
public class ClassThatThrowsException {
ClassThatThrowsException() {
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler());
}
public void doSomething() {
throw new CommonException("Something boring blew up!");
}
}
public class ClassThatThrowsExceptionTest {
#Test(expected=SuperAwesomeException.class)
public void testAwesome() {
ClassThatThrowsException c = new ClassThatThrowsException();
c.doSomething();
}
}
which throws:
java.lang.Exception: Unexpected exception, expected<SuperAwesomeException> but was<CommonException>
The problem is: when you are using JUnit, the framework will catch your exception. Therefore the uncaught exception handler isn't called in the first place!
See here for more details.
Thus, you have to do two things:
A) write tests that make sure that your exception handler implementation works as desired
#Test(expected=SuperAwesomeException.class)
public void testAwesome() {
new ExceptionHandler().uncaughtException(null, new CommonException("whatever"));
}
B) thest the plumbing - you want to make sure that this specific uncaught handler gets actually set by your code:
#Test
public void testDefaultHandlerIsSet() {
// creating a new instance should update the handler!
new ClassThatThrowsException();
Thread.UncaughtExceptionHandler handler = Thread.getDefaultUncaughtExceptionHandler();
assertThat(handler, not(nullValue()));
assertThat(handler, instanceOf(ExceptionHandler.class));
}
Finally - please note: you should not just do new XException(oldException.getMessage). Rather go for new XException("some message, oldException).
In other words: you got a cause here; so you better use the incoming exception as cause within the new one you intend to throw. Otherwise you loose all stack trace information.
I want a shared library to wrap the exceptions from within and re-throw it as an application specific exception for those that can't handle them.
What I currently have is a library exception with a static method that sets the application specific exception class that I instantiate via reflection in case a exception being thrown.
My goal is to have a single app specific exception that can be handled gracefully by a common code.
Is there a better way to do this?
lib.LibExUtil.java
class LibExUtil {
static Class<? extends RuntimeException> ex = RuntimeException.class;
public static setAppEx(Class<? extends RuntimeException> ex) {
this.ex = ex;
}
static RuntimeException wrap(Throwable t) {
return (RuntimeException)ex
.getDeclaredConstructor(new Class[] {Throwable.class}).newInstance(new Object[] {t})
}
}
lib.SomeUtil.java
static void utilMethod() {
try {
// code with checked exception
} catch (Exception ex) {
throw LibExUtil.wrap(ex);
}
}
myApp.MyAppEx.java
public class MyAppEx extends RuntimeException {
MyAppEx(Throwable t) {super(t);}
}
myApp.Client.java
// initialization
LibExUtil.setAppEx(myApp.MyAppEx.class); // optional
void method() {
SomeUtil.utilMethod();
}
For maintainability you generally want to avoid app specific functionality in a shared library.
It would make more sense to throw a library specific exception and wrap it at the app level if that is your use case.
Library exception:
public class LibraryException extends Exception {
LibraryException(Throwable t) {super(t);}
}
lib.SomeUtil.java
static void utilMethod() {
try {
// code with checked exception
} catch (Exception ex) {
throw new LibraryException(ex);
}
}
myApp.Client.java
void method() {
try{
SomeUtil.utilMethod();
} catch (LibraryException e) {
throw new MyAppEx(e);
}
}
In your example your library method SomeUtil.utilMethod(); has done some work and which resulted in an exception. It would be better that util method through some library defined exception alert the user that what went wrong with it.
Another issue is in case of multi project setup like maven if one module wants to throw a different exception then other module and the developer forgets to call static method then your library method will throw some misleading exception. Avoid this pattern.
I'm a newbie in android and I always see Exception when I'm running my code.
So, somebody can tell me Can I call a method before app go to crash anywhere without "try-catch".
This would be better way to handle uncaught exception:
public class MyApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
appInitialization();
}
private void appInitialization() {
defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(_unCaughtExceptionHandler);
}
private UncaughtExceptionHandler defaultUEH;
// handler listener
private Thread.UncaughtExceptionHandler _unCaughtExceptionHandler = new Thread.UncaughtExceptionHandler() {
#Override
public void uncaughtException(Thread thread, Throwable ex) {
ex.printStackTrace();
// TODO handle exception here
}
};
}
Application is a Base class for those who need to maintain global application state. And hence here, it will be a better place to handle such exceptions.
EDIT:
The above code will handle uncaught exceptions if they are thrown inside UI thread.
If an exception has occurred in worker thread, you can handle it in following way:
private boolean isUIThread(){
return Looper.getMainLooper().getThread() == Thread.currentThread();
}
// Setup handler for uncaught exceptions.
Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler()
{
#Override
public void uncaughtException (Thread thread, Throwable e)
{
handleUncaughtException (thread, e);
}
});
public void handleUncaughtException(Thread thread, Throwable e) {
e.printStackTrace(); // not all Android versions will print the stack trace automatically
if (isUIThread()) {
// exception occurred from UI thread
invokeSomeActivity();
} else { //handle non UI thread throw uncaught exception
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
invokeSomeActivity();
}
});
}
}
I think what you search is the UncaughtExceptionHandler. It is notified whenever an Exception is fired and not catched on its way bubbling up through your application.
See http://www.intertech.com/Blog/android-handling-the-unexpected/ for more details on implementing this in android.
Try this way
1) create class
import android.content.Context;
import android.content.Intent;
import android.os.Process;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.Thread.UncaughtExceptionHandler;
public class CrashReportHandler implements UncaughtExceptionHandler {
public static void attach(Context context) {
Thread.setDefaultUncaughtExceptionHandler(
new CrashReportHandler(context)
);
}
///////////////////////////////////////////// implementation
private CrashReportHandler(Context context) {
m_context = context;
}
public void uncaughtException(Thread thread, Throwable exception) {
StringWriter stackTrace = new StringWriter();
exception.printStackTrace(new PrintWriter(stackTrace));
//You will get call back here when app crashes.
// from RuntimeInit.crash()
Process.killProcess(Process.myPid());
System.exit(10);
}
private Context m_context;
}
How to use this class?
write this line in your activity onCreate() method
CrashReportHandler.attach(this);
There is method called Uncaught exception which is called just before force close dialog , you can write ur piece of code there .. Please check Using Global Exception Handling on android
Use CrashLytics for crash reporter free of cost and easy to implement
https://www.crashlytics.com/
We'd like a trace in our application logs of these exceptions - by default Java just outputs them to the console.
Since Java 7, you have to do it differently as the sun.awt.exception.handler hack does not work anymore.
Here is the solution (from Uncaught AWT Exceptions in Java 7).
// Regular Exception
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler());
// EDT Exception
SwingUtilities.invokeAndWait(new Runnable()
{
public void run()
{
// We are in the event dispatching thread
Thread.currentThread().setUncaughtExceptionHandler(new ExceptionHandler());
}
});
There is a distinction between uncaught exceptions in the EDT and outside the EDT.
Another question has a solution for both but if you want just the EDT portion chewed up...
class AWTExceptionHandler {
public void handle(Throwable t) {
try {
// insert your exception handling code here
// or do nothing to make it go away
} catch (Throwable t) {
// don't let the exception get thrown out, will cause infinite looping!
}
}
public static void registerExceptionHandler() {
System.setProperty('sun.awt.exception.handler', AWTExceptionHandler.class.getName())
}
}
A little addition to shemnons anwer:
The first time an uncaught RuntimeException (or Error) occurs in the EDT it is looking for the property "sun.awt.exception.handler" and tries to load the class associated with the property. EDT needs the Handler class to have a default constructor, otherwise the EDT will not use it.
If you need to bring a bit more dynamics into the handling story you are forced to do this with static operations, because the class is instantiated by the EDT and therefore has no chance to access other resources other than static. Here is the exception handler code from our Swing framework we are using. It was written for Java 1.4 and it worked quite fine there:
public class AwtExceptionHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(AwtExceptionHandler.class);
private static List exceptionHandlerList = new LinkedList();
/**
* WARNING: Don't change the signature of this method!
*/
public void handle(Throwable throwable) {
if (exceptionHandlerList.isEmpty()) {
LOGGER.error("Uncatched Throwable detected", throwable);
} else {
delegate(new ExceptionEvent(throwable));
}
}
private void delegate(ExceptionEvent event) {
for (Iterator handlerIterator = exceptionHandlerList.iterator(); handlerIterator.hasNext();) {
IExceptionHandler handler = (IExceptionHandler) handlerIterator.next();
try {
handler.handleException(event);
if (event.isConsumed()) {
break;
}
} catch (Throwable e) {
LOGGER.error("Error while running exception handler: " + handler, e);
}
}
}
public static void addErrorHandler(IExceptionHandler exceptionHandler) {
exceptionHandlerList.add(exceptionHandler);
}
public static void removeErrorHandler(IExceptionHandler exceptionHandler) {
exceptionHandlerList.remove(exceptionHandler);
}
}
Hope it helps.
There are two ways:
/* Install a Thread.UncaughtExceptionHandler on the EDT */
Set a system property:
System.setProperty("sun.awt.exception.handler",MyExceptionHandler.class.getName());
I don't know if the latter works on non-SUN jvms.
--
Indeed, the first is not correct, it's only a mechanism for detecting a crashed thread.