简体   繁体   English

java抛出异常而不捕获它?

[英]java throwing exception without catching it?

Is it possible to throw an exception without catching it?是否可以在不捕获异常的情况下抛出异常?

Example例子

public void foo() throws SomeException{
    // ....
    if (somethingCatestrophic) throw new SomeException();
    // ....
}

Now I want to call foo, but don't want to catch any errors, as the exceptions should never been thrown at runtime (unless there's a bug)现在我想调用 foo,但不想捕获任何错误,因为在运行时不应该抛出异常(除非有错误)

除非您正在计划并从本地恢复,否则在这种情况下最好使用未经检查的异常,例如RuntimeException派生类。

You can avoid catching an exception, but if there is an exception thrown and you don't catch it your program will cease execution (crash).您可以避免捕获异常,但如果抛出异常而您没有捕获它,您的程序将停止执行(崩溃)。

There is no way to ignore an exception.没有办法忽略异常。 If your app doesn't need to do anything in response to a given exception, then you would simply catch it, and then do nothing.如果您的应用程序不需要对给定的异常做任何响应,那么您只需捕获它,然后什么都不做。

try {
  ...some code that throws an exception...
} catch (SomeException ex) {
  // do nothing
}

NOTE: This is often considered bad style, however, and people may tell you so.注意:然而,这通常被认为是不好的风格,人们可能会这样告诉你。 The often-cited reason is that, even if you're not going to do anything with the exception, that in most cases you should at least log it somewhere, notify the user, or take some other appropriate action depending on what you app is doing, and what caused the exception in the first place.经常被引用的原因是,即使您不打算做任何例外的事情,在大多数情况下,您至少应该将其记录在某个地方,通知用户,或根据您的应用程序采取一些其他适当的操作在做什么,以及首先是什么导致了异常。 If you're not sure why an exception is being thrown (maybe it's a bug you haven't solved yet), then generally you should at least log it so you can figure it out later.如果您不确定抛出异常的原因(可能是您尚未解决的错误),那么通常您至少应该记录它,以便您以后可以弄清楚。

Why don't you catch it inside the method?为什么不在方法中捕获它?

Simply use try catch block and go on, if the exception is insignificant and doesn't influence any behaviour of your program.如果异常无关紧要并且不影响程序的任何行为,只需使用 try catch 块并继续。

There is a trick, You can play with generics.有一个技巧,你可以玩泛型。

/**
 * A java syntax glitch to throw any throwable without the need to catch it.
 *
 * @param throwable to be ignite
 * @param <T>       the type of the throwable to trick the compiler that it's the one thrown
 * @throws T exactly the given throwable
 */
public static <T extends Throwable> void ignite(Throwable throwable) throws T {
    Objects.requireNonNull(throwable, "throwable");
    throw (T) throwable;
}

This test should pass这个测试应该通过

@Test(expected = IOException.class)
public void ignite() {
    ignite(new IOException());
}

If SomeException is a checked exception, the method that calls foo() will either have to catch that exception and deal with it or also be declared to throw SomeException or a parent of it.如果SomeException是已检查异常,则调用foo()的方法要么必须捕获该异常并对其进行处理,要么声明为抛出SomeException或其父SomeException

If SomeException is a runtime exception, then methods that call it will not need to catch it.如果SomeException是运行时异常,则调用它的方法将不需要捕获它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM