java - 可以在 catch 块中访问 try 块中定义的标签吗?
问题描述
try {
do {
input: if (choice2 != null)
System.out.println("Please enter a valid choice");
choice2 = reader.readLine();
} while ((int) choice2.charAt(0) >= 65 && (int) choice2.charAt(0) <= 69);
} catch (StringIndexOutOfBoundsException e) {
continue input;
}
反正有没有实现类似的功能?上面的代码是一个例子。
解决方案
将try-catch
移入循环体。而且您可能打算标记循环。而且,即使它们是可选的,也要使用大括号。喜欢,
input: do {
try {
if (choice2 != null) {
System.out.println("Please enter a valid choice");
}
choice2 = reader.readLine();
} catch (StringIndexOutOfBoundsException e) {
continue input;
}
} while ((int) choice2.charAt(0) >= 65 && (int) choice2.charAt(0) <= 69);
推荐阅读
- powerbi - 如何将字符串列格式化为 int 列?
- prebid.js - 为出价设置自定义参数,并在响应中取回它们
- javascript - 选中复选框时如何更改文本显示
- mysql - 即使凭据保存在 /home/user/.my.cnf,mysqldump 也会提示密码
- printing - 如何将打印机驱动程序参数放在 postscript 打印文件中?
- csv - JMeter - CSV 数据集 Config- 第二次迭代的空白变量
- javascript - 在通过 CSS 转换其尺寸时获取元素的(最终)高度
- r - 后分布
- javascript - 传单标记集群与 geoJSON 数据过滤发生冲突
- java - 在本地运行应用程序时无法从资源文件夹加载文件