java中try-catch和throw的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3794910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between try-catch and throw in java
提问by user393043
What is the difference between try-catch and throw clause. When to use these?
try-catch 和 throw 子句有什么区别。什么时候使用这些?
Please let me know .
请告诉我 。
回答by Colin Hebert
- The
try
block will execute a sensitive code which can throw exceptions - The
catch
block will be used whenever an exception (of the type caught) is thrown in the try block - The
finally
block is called in everycase after the try/catch blocks. Even if the exception isn't caught or if your previous blocks break the execution flow. - The
throw
keyword will allow you to throw an exception (which will break the execution flow and can be caught in acatch
block). - The
throws
keyword in the method prototype is used to specify that your method might throw exceptions of the specified type. It's useful when you have checked exception (exception that you have to handle) that you don't want to catch in your current method.
- 该
try
块将执行可能引发异常的敏感代码 - 的
catch
任何时间(捕获的类型)的异常在try块被抛出块将被用于 - 该
finally
块被称为在每一个的try / catch块后的情况。即使未捕获到异常或者您之前的块中断了执行流程。 - 该
throw
关键字将允许您抛出异常(这将中断执行流程并可以在catch
块中捕获)。 throws
方法原型中的关键字用于指定您的方法可能会抛出指定类型的异常。当您检查了不想在当前方法中捕获的异常(您必须处理的异常)时,它很有用。
Resources :
资源 :
On another note, you should really accept some answers. If anyone encounter the same problems as you and find your questions, he/she will be happy to directly see the right answer to the question.
另一方面,你真的应该接受一些答案。如果有人遇到与您相同的问题并找到您的问题,他/她将很乐意直接看到问题的正确答案。
回答by Mahesh
Try/catch and throw clause are for different purposes. So they are not alternative to each other but they are complementary.
Try/catch 和 throw 子句用于不同的目的。所以它们不是相互替代的,而是互补的。
If you have throw some checked exception in your code, it should be inside some try/catch in codes calling hierarchy.
Conversely, you need try/catch block only if there is some throw clause inside the code (your code or the API call) that throws checked exception.
如果您在代码中抛出了一些已检查的异常,它应该在调用层次结构的代码中的一些 try/catch 内。
相反,仅当代码(您的代码或 API 调用)中有一些 throw 子句会引发已检查异常时,才需要 try/catch 块。
Sometimes, you may want to throw exception if particular condition occurred which you want to handle in calling code block and in some cases handle some exception catch block and throw a same or different exception again to handle in calling block.
有时,您可能希望在发生特定情况时抛出异常,您希望在调用代码块中处理这些情况,并且在某些情况下处理某些异常捕获块并再次抛出相同或不同的异常以在调用块中处理。
回答by user1864519
If you execute the following example, you will know the difference between a Throw and a Catch block.
如果您执行以下示例,您就会知道 Throw 和 Catch 块之间的区别。
In general terms:
笼统:
The catchblock will handle the Exception
在捕捉块将处理异常
throwswill pass the error to his caller.
throws会将错误传递给他的调用者。
In the following example, the error occurs in the throwsMethod()but it is handled in the catchMethod().
在以下示例中,在发生了错误throwsMethod() ,但它是在处理catchMethod() 。
public class CatchThrow {
private static void throwsMethod() throws NumberFormatException {
String intNumber = "5A";
Integer.parseInt(intNumber);
}
private static void catchMethod() {
try {
throwsMethod();
} catch (NumberFormatException e) {
System.out.println("Convertion Error");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
catchMethod();
}
}
回答by Ajinkya Khedkar
try - Add sensitive code catch - to handle exception finally - always executed whether exception caught or not. Associated with try -catch. Used to close the resource which we opened in try block throw - To handover our created exception to JVM manually. Used to throw customized exception throws - To delegate the responsibility of exception handling to caller method or main method.
try - 添加敏感代码 catch - 最后处理异常 - 无论异常是否被捕获,总是执行。与 try -catch 相关联。用于关闭我们在 try 块 throw 中打开的资源 - 手动将我们创建的异常移交给 JVM。用于抛出自定义异常抛出 - 将异常处理的责任委托给调用方方法或主方法。
回答by Ellen gomez
All these keywords try, catch and throw are related to the exception handling concept in java. An exception is an event that occurs during the execution of programs. Exception disrupts the normal flow of an application. Exception handling is a mechanism used to handle the exception so that the normal flow of application can be maintained. Try-catch block is used to handle the exception. In a try block, we write the code which may throw an exception and in catch block we write code to handle that exception. Throw keyword is used to explicitly throw an exception. Generally, throw keyword is used to throw user defined exceptions.
所有这些关键字 try、catch 和 throw 都与 java 中的异常处理概念有关。异常是在程序执行期间发生的事件。异常会破坏应用程序的正常流程。异常处理是一种用于处理异常以维持应用程序正常流程的机制。Try-catch 块用于处理异常。在 try 块中,我们编写可能引发异常的代码,而在 catch 块中,我们编写代码来处理该异常。Throw 关键字用于显式抛出异常。通常,throw 关键字用于抛出用户定义的异常。
For more detail visit Java tutorial for beginners.
有关更多详细信息,请访问面向初学者的 Java 教程。
回答by BNeumann
Others have already given thorough answers, but if you're looking for even more information, the Oracle Java tutorials are always a good resource. Here's the Java tutorial for Exceptions, which covers all of your questions in great detail; https://docs.oracle.com/javase/tutorial/essential/exceptions/index.html
其他人已经给出了详尽的答案,但如果您正在寻找更多信息,Oracle Java 教程始终是一个很好的资源。这是异常的 Java 教程,它非常详细地涵盖了您的所有问题;https://docs.oracle.com/javase/tutorial/essential/exceptions/index.html