Why does try-catch not catch my exceptions?

I replicated the issue in the basic workflow attached:

I am throwing an Exception in a Java snippet within my try-catch nodes, but even though the node does indeed fail, it is not being picked up in the catch node.

trycatch_test.knwf (15.1 KB)

Hi @Zvereec1 , this is because you effectively have a compiler error, which is not the same as a runtime error.


In a java program (or the Java Snippet), all Exceptions that are thrown MUST be caught. This is one of the things that arguably makes Java programs much more robust, than (for example) Python: because such “coding/design-errors” are caught by the compiler rather than having to wait until runtime.

However, Exception is a subclass of Throwable which is what would be caught outside of the snippet (if it were allowed to compile) , and the other subclass of Throwable is Error, which you are allowed to Throw without catching.

So change your code to throw an Error instead of an Exception, and it will be allowed to execute and then be caught by try-catch

I admit there is a slight irony that the message that appears when you throw an Error is “Exceptions in the code” :wink:


Hi @Zvereec1,

adding to the comment from @takbb you might want to use the breakpoint node, ideally placed right after the try node, to test your catch branch for error handling:


1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.