简体   繁体   English

引发Java异常

[英]Throwing Java Exceptions

When a method throws and exception, do we need to have a try block inside the method? 当方法抛出异常时,我们是否需要在方法内部使用try块?

For example, 例如,

    public void foo() throws SomeException{
        try{
            // content of method
        }
    }

Is the try block required? 是否需要try块? Or, is the method able to throw a SomeException without it : 或者,该方法是否可以抛出SomeException而没有它:

    public void foo() throws SomeException{
            // content of method
    }

This is the case when we are not explicitly throwing a SomeException with throw . 当我们没有使用throw显式抛出SomeException时就是这种情况。

If SomeException is a checked exception you have to either 如果SomeException检查异常 ,则必须

  • Use a try{}catch block or 使用try{}catch
  • Declare that your method throws it. 声明您的方法将其抛出。

You do not have to do both, either example you show in your question works just fine. 您不必同时做这两个事情,您在问题中显示的任何一个例子都很好。

The difference is that with the try clause you handle the SomeException yourself, whereas by declaring that your own method throws it you delegate the responsability of handling the SomeException to the calling method. 区别在于,使用try子句可以自己处理SomeException ,而通过声明自己的方法throwsthrows则可以将处理SomeException的责任委托给调用方法。

When a method throws an exception it passes responsibility to handle exception to its caller. 当方法抛出异常时,它会将负责处理异常的责任传递给其调用方。 So you don't need to handle exception if you throw it in your signature. 因此,如果将异常放入签名中,则无需处理异常。 Like as follows. 如下所示。

public void foo(){
        try{
            // content of method
        }
    }

but if you write it this way. 但是如果你这样写的话

public void foo() throws SomeException{

    }

you will call your method like as follows. 您将如下所示调用您的方法。

try{

   foo();
}

1. If the method that we are calling from a program throws an Exception , then we need to use try/catch around the method invocation. 1.如果我们从程序调用的方法throws an Exception则我们需要 在方法调用周围 使用 try/catch

2. Suppose we are writing a method that throws an exception , then we need to throw new Exception object from withing the method. 2.假设我们正在编写一个 throws an exception 的方法那么我们需要 从该方法中 throw new Exception object

3. An exception is an object of type Exception . 3.异常是Exception类型的对象 We have Checked Exception , and Unchecked Exception (Runtime Exception) . 我们有Checked ExceptionUnchecked Exception (Runtime Exception)

You don't need a try block. 您不需要try块。

public void foo() throws SomeException {
  // do some stuff
  // you decide to throw the exception by yourself:
  if (throwAnException) throw new SomeException();
  // or you call a method that throws SomeExpection:
  methodThatCanThrowSomeException();
  // do more stuff
}

As long as you declare it in your signature, you're prefectly fine. 只要您在签名中声明,就可以了。 The caller of your method has to handle the exception, not you. 方法的调用者必须处理异常,而不是您。 So a caller might do: 因此,呼叫者可能会这样做:

try {
  foo();
} catch (SomeException e) {
  // handle exception
}

Or he might pass it further along by himself. 否则他可能会自己进一步传递。

The most problematic case you'll regularly encounter is calling a method that declares a checked exception. 您经常遇到的最棘手的情况是调用一个声明已检查异常的方法。 In the great majority of real-life cases it is not appropriate to handle that exception at the spot, but let it propagate upwards. 在现实生活中的绝大多数情况下,不适合现场处理该异常,而应使其向上传播。 Unfortunately, Java makes you redeclare this same exception all the way up, which creates clutter, exposes implementation details, and often also breaks the contracts of existing methods. 不幸的是,Java使您一直重新声明相同的异常,这会造成混乱,暴露实现细节,并且通常还会破坏现有方法的契约。

In such a case the way to proceed is to wrap and rethrow : 在这种情况下,进行的方法是包装并重新抛出

catch (RuntimeException e) {throw e;} catch (Exception e) {throw new RuntimeException(e);}

you don't essentially need to have a try block in it 您基本上不需要在其中尝试块

      public void foo() throws SomeException {
// do some stuff
// you decide to throw the exception by yourself:
if (throwAnException) throw new SomeException();
// or you call a method that throws SomeExpection:
methodThatCanThrowSomeException();
// do more stuff
 }

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

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