@ExceptionResolver

@ShellComponent 类可以使用 @ExceptionResolver 方法来处理组件方法的异常。这些方法适用于带注解的方法。

@ShellComponent classes can have @ExceptionResolver methods to handle exceptions from component methods. These are meant for annotated methods.

该异常可能与传播的顶级异常相匹配(例如,直接抛出的 IOException),或与包装器异常中的嵌套原因相匹配(例如,包装在 IllegalStateException 中的 IOException)。它可以在任意原因级别匹配。

The exception may match against a top-level exception being propagated (e.g. a direct IOException being thrown) or against a nested cause within a wrapper exception (e.g. an IOException wrapped inside an IllegalStateException). This can match at arbitrary cause levels.

对于匹配的异常类型,最好将目标异常声明为方法参数,如前面的示例所示。如果匹配多个异常方法,通常优先匹配根异常而不是原因异常。更具体地说,ExceptionDepthComparator 会根据异常深度对异常进行排序,从而从抛出异常的类型开始排序。

For matching exception types, preferably declare the target exception as a method argument, as the preceding example(s) shows. When multiple exception methods match, a root exception match is generally preferred to a cause exception match. More specifically, the ExceptionDepthComparator is used to sort exceptions based on their depth from the thrown exception type.

或者,注释声明可以缩小要匹配的异常类型,如下例所示:

Alternatively, the annotation declaration may narrow the exception types to match, as the following example shows:

Unresolved include directive in modules/ROOT/pages/commands/exceptionhandling/annotation.adoc - include::../../test/java/org/springframework/shell/docs/ErrorHandlingSnippets.java[]
Unresolved include directive in modules/ROOT/pages/commands/exceptionhandling/annotation.adoc - include::../../test/java/org/springframework/shell/docs/ErrorHandlingSnippets.java[]

@ExceptionResolver 还可以返回 String,后者可用作控制台输出。您可以使用 @ExitCode 注释来定义返回代码。

@ExceptionResolver can also return String which is used as an output to console. You can use @ExitCode annotation to define return code.

Unresolved include directive in modules/ROOT/pages/commands/exceptionhandling/annotation.adoc - include::../../test/java/org/springframework/shell/docs/ErrorHandlingSnippets.java[]

@ExceptionResolvervoid 返回类型一起自动被处理为已处理的异常。然后,您还可以定义 @ExitCode,并在需要向控制台写入内容时使用 Terminal

@ExceptionResolver with void return type is automatically handled as handled exception. You can then also define @ExitCode and use Terminal if you need to write something into console.

Unresolved include directive in modules/ROOT/pages/commands/exceptionhandling/annotation.adoc - include::../../test/java/org/springframework/shell/docs/ErrorHandlingSnippets.java[]

Method Arguments

@ExceptionResolver 方法支持以下参数:

@ExceptionResolver methods support the following arguments:

Method argument Description

Exception type

For access to the raised exception. This is any type of Exception or Throwable.

Terminal

For access to underlying JLine terminal to i.e. get its terminal writer.

Return Values

@ExceptionResolver 方法支持以下返回值:

@ExceptionResolver methods support the following return values:

Return value Description

String

Plain text to return to a shell. Exit code 1 is used in this case.

CommandHandlingResult

Plain CommandHandlingResult having message and exit code.

void

A method with a void return type is considered to have fully handled the exception. Usually you would define Terminal as a method argument and write response using terminal writer from it. As exception is fully handled, Exit code 0 is used in this case.