{"record":{"id":"2584e16ff020c3a3","repo":"greenrobot/EventBus","slug":"failure-event-class-must-have-a-constructor-with-o","errorCode":null,"errorMessage":"Failure event class must have a constructor with one parameter of type Throwable","messagePattern":"Failure event class must have a constructor with one parameter of type Throwable","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"EventBus/src/org/greenrobot/eventbus/util/AsyncExecutor.java","lineNumber":107,"sourceCode":"    }\n\n    public static AsyncExecutor create() {\n        return new Builder().build();\n    }\n\n    private final Executor threadPool;\n    private final Constructor<?> failureEventConstructor;\n    private final EventBus eventBus;\n    private final Object scope;\n\n    private AsyncExecutor(Executor threadPool, EventBus eventBus, Class<?> failureEventType, Object scope) {\n        this.threadPool = threadPool;\n        this.eventBus = eventBus;\n        this.scope = scope;\n        try {\n            failureEventConstructor = failureEventType.getConstructor(Throwable.class);\n        } catch (NoSuchMethodException e) {\n            throw new RuntimeException(\n                    \"Failure event class must have a constructor with one parameter of type Throwable\", e);\n        }\n    }\n\n    /** Posts an failure event if the given {@link RunnableEx} throws an Exception. */\n    public void execute(final RunnableEx runnable) {\n        threadPool.execute(() -> {\n            try {\n                runnable.run();\n            } catch (Exception e) {\n                Object event;\n                try {\n                    event = failureEventConstructor.newInstance(e);\n                } catch (Exception e1) {\n                    eventBus.getLogger().log(Level.SEVERE, \"Original exception:\", e);\n                    throw new RuntimeException(\"Could not create failure event\", e1);\n                }\n                if (event instanceof HasExecutionScope) {","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/greenrobot/EventBus/blob/0194926b3bcf70cc0d7bfd3c5da16708dd5ab876/EventBus/src/org/greenrobot/eventbus/util/AsyncExecutor.java#L89-L125","documentation":"Thrown as RuntimeException from the AsyncExecutor constructor when failureEventType.getConstructor(Throwable.class) throws NoSuchMethodException. AsyncExecutor's contract: when a RunnableEx passed to execute() throws, AsyncExecutor instantiates the configured failure event class with new FailureEvent(Throwable cause) and posts it. The constructor lookup happens once at AsyncExecutor creation; a failure event without a public single-Throwable constructor makes the mechanism unusable, so construction fails fast.","triggerScenarios":"Building an AsyncExecutor via AsyncExecutor.builder().eventBus(bus).failureEventTypeId / .buildForScope(scope) with a failure event class that has no constructor, a multi-arg constructor, a (String) constructor, or a non-public (private/protected/package-private) Throwable constructor.","commonSituations":"Writing a custom failure event with extra fields and forgetting the Throwable constructor; Kotlin data classes whose only constructor takes additional parameters; making the Throwable constructor private for a factory-pattern class; reusing an arbitrary event class not designed for AsyncExecutor.","solutions":["Add a public constructor taking exactly one Throwable to the failure event class: public FailureEvent(Throwable throwable) { this.cause = throwable; }","In Kotlin: class FailureEvent(val throwable: Throwable) (primary constructor works) or add a secondary constructor","Prefer subclassing the provided org.greenrobot.eventbus.util.ThrowableFailureEvent which already defines the required constructor"],"exampleFix":"// before\npublic class MyFailureEvent {\n    public String message;\n    public MyFailureEvent(String message) { this.message = message; } // no Throwable ctor -> throws\n}\n\n// after\npublic class MyFailureEvent extends org.greenrobot.eventbus.util.ThrowableFailureEvent {\n    public MyFailureEvent(Throwable throwable) { super(throwable); }\n}","handlingStrategy":"validation","validationCode":"// Verify the failure event contract before building the executor\ntry {\n    failureEventType.getConstructor(Throwable.class);\n} catch (NoSuchMethodException e) {\n    throw new IllegalStateException(\n        failureEventType.getName() + \" needs a public constructor taking exactly one Throwable\", e);\n}\nAsyncExecutor exec = AsyncExecutor.builder()\n    .eventBus(EventBus.getDefault())\n    .failureEventType(failureEventType)\n    .buildForScope(this);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Extend org.greenrobot.eventbus.util.ThrowableFailureEvent instead of writing custom classes","Write a one-line unit test asserting getConstructor(Throwable.class) succeeds for your failure event"],"tags":["eventbus","asyncexecutor","constructor","failure-event","util"],"backgroundTag":null,"analyzedSha":"0194926b3bcf70cc0d7bfd3c5da16708dd5ab876","analyzedAt":"2026-08-14T10:41:08.786Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}