|
22 | 22 | import javax.lang.model.element.Element; |
23 | 23 | import javax.lang.model.element.ExecutableElement; |
24 | 24 |
|
| 25 | +import com.sun.codemodel.JCatchBlock; |
| 26 | +import com.sun.codemodel.JStatement; |
| 27 | +import com.sun.codemodel.JTryBlock; |
| 28 | +import com.sun.codemodel.JVar; |
25 | 29 | import org.androidannotations.annotations.Background; |
26 | 30 | import org.androidannotations.api.BackgroundExecutor; |
27 | 31 | import org.androidannotations.api.BackgroundExecutor.Task; |
@@ -58,7 +62,17 @@ public void process(Element element, EComponentHolder holder) throws Exception { |
58 | 62 | JMethod executeMethod = anonymousTaskClass.method(JMod.PUBLIC, codeModel().VOID, "execute"); |
59 | 63 | executeMethod.annotate(Override.class); |
60 | 64 |
|
61 | | - executeMethod.body().add( previousMethodBody ); |
| 65 | + // Catch exception in user code |
| 66 | + JTryBlock tryBlock = executeMethod.body()._try(); |
| 67 | + tryBlock.body().add(previousMethodBody); |
| 68 | + JCatchBlock catchBlock = tryBlock._catch(holder.classes().THROWABLE); |
| 69 | + JVar caughtException = catchBlock.param("e"); |
| 70 | + JStatement uncaughtExceptionCall = holder.classes().THREAD |
| 71 | + .staticInvoke("getDefaultUncaughtExceptionHandler") |
| 72 | + .invoke("uncaughtException") |
| 73 | + .arg(holder.classes().THREAD.staticInvoke("currentThread")) |
| 74 | + .arg(caughtException); |
| 75 | + catchBlock.body().add(uncaughtExceptionCall); |
62 | 76 |
|
63 | 77 | Background annotation = element.getAnnotation(Background.class); |
64 | 78 | String id = annotation.id(); |
|
0 commit comments