“执行多次捕获异常策略" [英] "Executed multiple catch exception strategy "
问题描述
我有选择异常策略,在这里我有多个catch异常策略,我正在根据错误应用条件.
I have choice exception strategy where I have multiple catch exception strategy there I am applying condition based on the error.
错误1:
org.mule.api.MessagingException:列'department_id'不能为null(java.sql.SQLIntegrityConstraintViolationException).
org.mule.api.MessagingException: Column 'department_id' cannot be null (java.sql.SQLIntegrityConstraintViolationException).
错误2:
org.mule.api.MessagingException:org.mule.module.db.internal.domain.connection.ConnectionCreationException:无法获得URL的连接jdbc:mysql://localhost:3306/mulesoft:用户'root1212的访问被拒绝'@'localhost'(使用密码:是)(java.sql.SQLException)(org.mule.module.db.internal.processor.DbConnectionException).
org.mule.api.MessagingException: org.mule.module.db.internal.domain.connection.ConnectionCreationException: Cannot get connection for URL jdbc:mysql://localhost:3306/mulesoft : Access denied for user 'root1212'@'localhost' (using password: YES) (java.sql.SQLException) (org.mule.module.db.internal.processor.DbConnectionException).
如何在catch异常策略中使用表达式来区分这两种错误?
How can I differentiate between both of the error using expression in catch exception strategy?
第一个捕获-在以下情况下执行:
First catch- Executed when:
[exception.causeMatches("org.mule.api.MessagingException:列'department_id'不能为空*")]
[exception.causeMatches("org.mule.api.MessagingException: Column 'department_id' cannot be null*")]
第二个捕获-在以下情况下执行:
Second catch- Executed when:
[exception.causeMatches("org.mule.api.MessagingException:org.mule.module.db.internal.domain.connection.ConnectionCreationException *")]
[exception.causeMatches("org.mule.api.MessagingException: org.mule.module.db.internal.domain.connection.ConnectionCreationException*")]
使用此方法无法触发捕获异常策略.
By using this not able to trigger into catch exception strategy.
[exception.causeMatches("org.mule.api.MessagingException *")]
[exception.causeMatches("org.mule.api.MessagingException*")]
这是有效的,但是对于两个错误都使用相同的起始字符串.我该如何区分两者?
This is working but for both the error getting same starting string. How can i differentiate both?
推荐答案
如果您想根据根本原因捕获特定的异常.在具有完全限定的异常名称的异常上使用causeBy方法.下面的示例处理2个特定的异常,然后默认捕获所有其他异常.
If you want to catch specific exceptions based on the root cause. Use causedBy method on exception with the fully qualified exception name. Below examples handles 2 specific exceptions and then a default catch for all other exceptions.
<choice-exception-strategy>
<catch-exception-strategy when="#[exception.causedBy(com.ricston.CustomException)">
<!-- do stuff -->
</catch-exception-strategy>
<catch-exception-strategy when="#[exception.causedBy(org.mule.module.db.internal.domain.connection.ConnectionCreationException)">
<!-- do stuff -->
</catch-exception-strategy>
<catch-exception-strategy>
<!-- other exceptions. do stuff -->
</catch-exception-strategy>
</choice-exception-strategy>
这篇关于“执行多次捕获异常策略"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!