简体   繁体   English

“throws”与方法名称和捕获异常有什么区别?

[英]what is difference between “throws” with the method name and catching an exception?

I am very new to exception handling in java, hence this question might seems stupid but please let it be answered. 我对java中的异常处理很新,因此这个问题可能看起来很愚蠢,但请让它回答。 Suppose i have a method A in which some part of code can throw an exception B then what is the difference between catching the exception in the method or writting the method declaration as:- 假设我有一个方法A ,其中代码的某些部分可以抛出异常B然后在方法中捕获异常或将方法声明写为以下内容之间有什么区别: -

void A() throws B{
     ----//----
     }

The difference lies in the way you call the method. 区别在于您调用方法的方式。

  1. If your method signature says throws Exception , the code that calls your method must deal with the Exception either by catching it [via a catch clause] or throw it back [via throws clause]. 如果你的方法签名表示throws Exception ,调用你的方法必须处理的代码Exception或者通过捕获它[通过catch条款]或扔回去[通过throws条款。

  2. If you catch the Exception , you are basically trying to handle the Exception yourself and the code that calls your method does not have to deal with the Exception . 如果您catch Exception ,您基本上是在尝试自己处理Exception并且调用您的方法的代码不必处理Exception

My advice is as follows: 我的建议如下:

  1. If you think you can handle and recover from the Exception , that you should catch and do the needful. 如果您认为可以处理并从Exception恢复,那么您应该catch并完成所需的工作。

  2. If you cannot deal with the Exception properly that do not catch it, you must throw it back. 如果你不能与处理Exception正确不catch它,你必须扔回去。

NOTE: It is a bad programming style to catch an Exception and re-throw it back [although you can do it]. 注意: catch Exception并重新抛出它是一种糟糕的编程风格[尽管你可以这样做]。 If you wrap the Exception with another one, then it is ok. 如果你用另一个包装Exception ,那就没关系。 But in this case it must make sense to wrap the Exception with another one. 但在这种情况下,用另一个包装Exception必须有意义。

Update: The idea is, no matter what number of layers your application has, somebody has to deal with that throws Exception clause. 更新:这个想法是,无论您的应用程序有多少层,有人必须处理throws Exception子句。 You cannot just let a StackTrace appear to the user in the middle of his activity with the application. 你不能让StackTrace在他的应用程序活动中出现给用户。 The point to consider is "Can you recover from the error and resume your processing?" 需要考虑的是“你能从错误中恢复并恢复处理吗?” If yes, then handle the Exception, else throw it back to the layer that can at least show a meaningful message to the user about what happened. 如果是,则处理异常,否则将其抛回到至少可以向用户显示有关发生的事情的有意义消息的层。

If you catch the exception in A() you have to react to the error in your method A() , for example by trying the offending code again, logging the error, etc. 如果在A()捕获异常,则必须对方法A()的错误作出反应,例如再次尝试违规代码,记录错误等。

If you (re)throw the exception, you expect/forcce the method that called A() to handle the exception or it goes unhandled. 如果您(重新)抛出异常,您希望/ forcce调用A()的方法来处理异常,或者它处于未处理状态。

The difference is that if you catch B in the method body, you're handling the exception. 不同之处在于,如果您在方法体中捕获B,则表示您正在处理异常。 You could throw another exception, but unless you do so the problem is considered solved as far as Java is concerned. 您可以抛出另一个异常,但除非您这样做,否则就Java而言,问题被认为已解决。 By using the throws declaration on the method signature, you're indicating that your method can throw exceptions of type B and other pieces of code using it will be forced to deal with it, or throw it further up. 通过在方法签名上使用throws声明,您将指示您的方法可以抛出类型B的异常,并且使用它的其他代码段将被强制处理它,或者进一步抛出它。

What you wrote is merely an exception specification , which tells everyone which exceptions you may possibly throw. 你写的只是一个异常规范 ,告诉每个人你可能会抛出哪些异常。 That doesn't mean that you will actually end up throwing any exceptions. 这并不意味着你实际上最终会抛出任何异常。

On the other hand, try / catch is about exception handling : You surround code in a try block which may throw an exception and then handle the exceptions as they come in. 另一方面, try / catch是关于异常处理的 :你可以在try块中包含代码,这可能会抛出异常,然后在异常进入时处理它们。

From inside your class A you are only allowed to throw exceptions which are listed in the exception specification; 从类A内部,您只能抛出异常规范中列出的异常; if A derives from another class or interface, A s specification must be at least as restrictive as that of the base. 如果A派生自另一个类或接口,则A规范必须至少与基础规范一样具有限制性。

When you throw the exception you are "throwing" it back to the caller of that method. 抛出异常时,您将其“抛出”回该方法的调用者。

On the other hand "catching" it with a try-catch block allows you to handle it within the method. 另一方面,使用try-catch块“捕获”它可以让你在方法中处理它。

try {
//Do your stuff here
}

catch (Exception ex) {
System.out.println("I just caught exception = " + ex);
}

Catching the exception means you handle it, and should be done if you can recover from the exception, or you want to rethrow another exception. 捕获异常意味着您可以处理它,如果您可以从异常中恢复,或者您想重新抛出另一个异常,则应该执行此操作。

Throwing the exception means letting the caller deal with it, should be used in any other case. 抛出异常意味着让调用者处理它,应该在任何其他情况下使用。

I would suggest you follow this lesson . 我建议你按照这一课

If you catch the exception in the method, you are essentially handling it (even if you do nothing with it) and the caller may not (necessarily) be aware; 如果你在方法中捕获异常,你实际上是在处理它(即使你什么也不做),调用者可能(必然)不知道; otherwise you are indicating to callers of the method what can go wrong, and what errors they're expected to handle, and if they don't handle it, then their application will certainly make them aware when the exception occurs. 否则你向调用者表明可能出错的方法,以及他们期望处理什么错误,如果他们不处理它,那么他们的应用程序肯定会让他们知道异常何时发生。

When you are catching the exception it means that you'll handle the exception somehow in your catch-part. 当您捕获异常时,这意味着您将以某种方式在catch部分中处理异常。 But when you are declaring that A() throws B you simply tell that A()-function might throw a B-exception. 但是当你声明A()抛出B时,你只是告诉A() - 函数可能抛出B异常。 So when somebody is using A()-function he knows that he must handle the B-exception or throw it forward. 所以当有人使用A() - 函数时,他知道他必须处理B异常或将其抛出。

  1. Catching the Exception is: You are providing a placeholder for handling the exceptional situation. 捕获异常是:您提供占位符来处理异常情况。 So if any part of code in try block throws this exception, it will come to the catch block and the piece of code will be executed. 因此,如果try块中的任何代码部分抛出此异常,它将进入catch块并执行该段代码。

  2. Throwing Exception is : You are not handling the exception. 抛出异常是:您没有处理异常。 So if the code in the method which throws exception in header, creates the mentioned exception, it will be thrown to the method stack. 因此,如果方法中的代码在头中抛出异常,则会创建提到的异常,它将被抛出到方法堆栈中。 so if code is like : 所以如果代码是这样的:

 method1() { x.method2(); } method2() { method3(); } method3() throws SomeException { ..... ...... } 

Then if the code in method3 throws SomeException, that will be propagated to method2, if method2 also does not handle the exception then to method1, likewise, up in the method stack. 然后,如果method3中的代码抛出SomeException,那么它将传播到method2,如果method2也不处理异常,那么同样在方法堆栈中处理method1。

暂无
暂无

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

相关问题 throws Throwable和throws Exception之间有什么区别 - What is the difference between throws Throwable and throws Exception 在javadoc中,标签@throws和@exception之间有什么区别? - In javadoc, what is the difference between the tags @throws and @exception? try-catch和throws Exception在性能方面有什么区别? - What is the difference between try-catch and throws Exception in terms of performance? throw和throws Exception有什么区别? 为什么“投掷”不需要接球/最终挡住? - What is the difference between throw and throws Exception? And why does “throws” not require a catch/finally block? 捕获异常或某些类型异常之间是否有任何性能差异? - Java - Is there any performance difference between catching Exception or certain type exception ? - Java Java - 抛出异常与捕获和重新抛出异常之间的区别 - Java - Difference between throwing an Exception and catching and rethrowing Exception 使用Exception类或FileNotFoundException类捕获异常之间的区别 - Difference between catching exceptions using Exception class or FileNotFoundException class 如果我在方法中“抛出新错误()”,在方法声明中附加和不附加“抛出错误”有什么区别? - If I "throw new Error()" in a method what is the difference between appending and not appending "throws Error" to the method declaration? 如果方法抛出未在方法声明中使用“throws”指定的异常,会发生什么情况 - What happens if a method throws an exception that was not specified in the method declaration with “throws” 运算符和方法有什么区别? - What is the difference between an Operator and Method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM