该版本仍在开发中,尚未被视为稳定。最新稳定版本请使用Spring Shell 3.4.1spring-doc.cadn.net.cn

@ExceptionResolver

@ShellComponent类可以有@ExceptionResolver处理组件异常的方法 方法。这些是为带注释的方法设计的。spring-doc.cadn.net.cn

异常可能与正在传播的顶层异常匹配(例如直接异常)IOException被抛掷)或针对包裹例外内嵌套原因(例如:IOException包裹 在非法州例外).这可以以任意的原因水平匹配。spring-doc.cadn.net.cn

对于匹配异常类型,最好将目标异常声明为方法参数,如 前面的例子展示了。当多个异常方法匹配时,根异常匹配为 通常比原因例外匹配更受青睐。更具体地说,是ExceptionDepthComparator用于根据异常与抛出异常类型的深度进行排序。spring-doc.cadn.net.cn

或者,注释声明可以缩小例外类型以匹配,因为 以下示例展示了:spring-doc.cadn.net.cn

@ExceptionResolver({ RuntimeException.class })
CommandHandlingResult errorHandler(Exception e) {
	// Exception would be type of RuntimeException,
	// optionally do something with it
	return CommandHandlingResult.of("Hi, handled exception\n", 42);
}
@ExceptionResolver
CommandHandlingResult errorHandler(RuntimeException e) {
	return CommandHandlingResult.of("Hi, handled custom exception\n", 42);
}

@ExceptionResolver也可以返回字符串该信号作为控制台的输出。您可以 用@ExitCode注释以定义返回码。spring-doc.cadn.net.cn

@ExceptionResolver
@ExitCode(code = 5)
String errorHandler(Exception e) {
	return "Hi, handled exception";
}

@ExceptionResolver无效返回类型会自动作为处理异常处理处理。 你也可以定义@ExitCode以及使用终端如果你需要写点什么 进入控制台:spring-doc.cadn.net.cn

@ExceptionResolver
@ExitCode(code = 5)
void errorHandler(Exception e, Terminal terminal) {
	PrintWriter writer = terminal.writer();
	String msg =  "Hi, handled exception " + e.toString();
	writer.println(msg);
	writer.flush();
}

方法参数

@ExceptionResolver方法支持以下论证:spring-doc.cadn.net.cn

方法论证 描述

例外类型spring-doc.cadn.net.cn

以获取提出的例外情况。这包括任何类型的例外可投掷.spring-doc.cadn.net.cn

终端spring-doc.cadn.net.cn

为了访问基础资产JLine从终端到,也就是说,获取它的终端写入者。spring-doc.cadn.net.cn

回报值

@ExceptionResolver方法支持以下返回值:spring-doc.cadn.net.cn

返回价值 描述

字符串spring-doc.cadn.net.cn

用纯文字返回壳体。此时使用出口代码1。spring-doc.cadn.net.cn

指令处理结果spring-doc.cadn.net.cn

平原指令处理结果有消息和退出代码。spring-doc.cadn.net.cn

无效spring-doc.cadn.net.cn

具有空返回类型的方法被视为已完全处理该异常。通常 你会定义终端作为方法参数,并用终端写入器编写响应。由于异常已被完全处理,此时使用出口代码0。spring-doc.cadn.net.cn