java错放了构造_在catch代码附近的Java代码中“错放了构造”(“misplaced construct(s)” in Java code near a catch)...

在catch代码附近的Java代码中“错放了构造”(“misplaced construct(s)” in Java code near a catch)

我无法在Java中编译以下代码,错误是:错误的构造(s)。 怎么了?

public class ExceptionsTutorial {

public static void main(String[] argv) throws Exception{

try{

System.out.println("A");

try{

System.out.println("B");

throw new Exception("1");

}

catch{

System.out.println("C");

throw new Exception("2");

}

finally{

System.out.println("D");

throw new Exception("3");

}

}

finally{

System.out.println("F");

}

}

}

I could not compile the following code in Java, error is: misplaced construct(s). What's wrong?

public class ExceptionsTutorial {

public static void main(String[] argv) throws Exception{

try{

System.out.println("A");

try{

System.out.println("B");

throw new Exception("1");

}

catch{

System.out.println("C");

throw new Exception("2");

}

finally{

System.out.println("D");

throw new Exception("3");

}

}

finally{

System.out.println("F");

}

}

}

原文:https://stackoverflow.com/questions/1690353

更新时间:2019-12-16 13:09

最满意答案

catch必须声明它捕获的异常:

catch (Exception E) {

System.out.println("C");

throw new Exception("2");

}

catch must declare what exception it catches:

catch (Exception E) {

System.out.println("C");

throw new Exception("2");

}

2009-11-06

相关问答

是的,尝试(使用Java)不会有任何性能影响。 编译器不会为try块生成虚拟机语句。 它只记录try块处于活动状态的程序计数器,并将该信息附加到类文件中的方法中。 然后,当引发异常时,VM展开堆栈并检查每个帧中该帧中的程序计数器是否在相关的try块中。 这(与建立堆栈跟踪一起)成本很高,因此捕获成本很高。 但是,尝试是免费的:)。 但是,对于常规控制流使用异常并不是一个好习惯。 您的代码执行速度更快的原因可能在于,捕获成本非常高,以至于通过简单的尝试来取代检查所节省的时间超过了其成本。 尝试捕捉可

...

is.read()声明它抛出IOException 。 InvalidClassException扩展IOException 。 因此,就您的代码所知, is.read()可能会抛出InvalidClassException 。 所以你可以尝试抓住它。 is.read() declares that it throws IOException. InvalidClassException extends IOException. Therefore as far as your code know

...

你的问题在这里: reset();//this makes the textField1 hold an empty String

operand=Double.parseDouble(textField1.getText());

因此,当您尝试从中读取并解析Double时,您会发现NumberFormatException,因为您无法将空String转换为Double。 你的问题的一个解决方案是创建第二个JTextField并让用户将他想要添加的第一个数字写入第一个textField,将第二

...

catch必须声明它捕获的异常: catch (Exception E) {

System.out.println("C");

throw new Exception("2");

}

catch must declare what exception it catches: catch (Exception E) {

System.out.println("C");

throw new Exception("2");

}

尝试这个 public static void main(String[] args) {

double wage = Validate.collectWage("Please enter your hourly wage");

}// end main method

public static Double collectWage(String messageIn) {

Scanner input = new Scanner(System.in);

String st

...

这听起来像是一个静态的横切关注点。 使用Java时,您可以使用AOP,aspectJ轻松添加此类问题(日志记录)。 这是定义正确的切入点的问题。 请注意,AOP通常在应用程序的设计和开发过程中使用。 我不知道你是否能用一个切入点实现你的目标。 此外,AOP可以是非侵入性的。 这意味着您不必更改应用程序的源代码。 它甚至可以在没有源代码时使用,例如只在JAR中编译的类。 This sounds like a static crosscutting concern. You can add this

...

说明 从Java文档 : [ try块]包含一个或多个可能抛出异常的法定代码行。 (接下来的两个小节将解释catch和finally块。) 例外是一种特殊的对象 。 当你编写new Exception() ,你正在创建一个新的异常对象。 当你写throw new Exception()你正在创建一个新的错误,然后把它扔到最近的try-catch块中,放弃你的其他代码。 当你抛出一个异常时,它被try-catch块捕获,它嵌套在 (在)内部。 也就是说,假定注册了该异常的正确catch块。 如果代码

...

由于不会在try块内部抛出异常的方法,是否会对以下代码产生任何负面的性能影响? 不,它不会有任何性能影响,但是: 无论如何,我通常会在try块中放入最少的代码 我会尽量避免捕获Exception ,而是捕获特定的异常 只是打印堆栈跟踪很少是正确的“处理”方式和您捕获的异常。 通常你想中止整个操作,不管是什么组成的 Will there be any negative performance implications on the following code due to methods that

...

您可以将Thread.sleep包装在一个函数中,该函数将任何异常重新抛出为运行时异常(或任何未捕获的异常)。 public static void trySleep(long millis) {

try {

Thread.sleep(millis);

} catch (InterruptedException e) {

throw new RuntimeException("Interrupted during sleep", e);

}

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值