在 java 中尝试做什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2589575/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:56:17  来源:igfitidea点击:

What does try do in java?

javaexception-handlingtry-catch

提问by David

What does trydo in java?

try在java中做什么?

回答by Thilo

The try/catch/finallyconstruct allows you to specify code that will run in case an exception has occured inside of the try block (catch), and/or code that will run after the try block, even if an exception has occured (finally).

try/catch/finally构造允许您指定在 try 块 ( catch)内发生异常时将运行的代码,和/或在 try 块之后运行的代码,即使发生了异常 ( finally)。

 try{
    // some code that could throw  MyException;
 }
 catch (MyException e){
     // this will be called when MyException has occured
 }
 catch (Exception e){
     // this will be called if another exception has occured
     // NOT for MyException, because that is already handled above
 }
 finally{
     // this will always be called,
     // if there has been an exception or not
     // if there was an exception, it is called after the catch block
 }

Finally blocks are important to release resources such as database connections or file handles no matter what. Without them, you would not have a reliable way to execute clean-up code in the presence of exceptions (or return, break, continue and so on out of the try block).

无论如何,finally 块对于释放资源(例如数据库连接或文件句柄)很重要。如果没有它们,您将无法在出现异常(或在 try 块外返回、中断、继续等)时执行清理代码的可靠方法。

回答by Michael Shimmins

It allows you to attempt an operation, and in the event an Exception is thrown, you can handle it gracefully rather than it bubbling up and being exposed to the user in an ugly, and often unrecoverable, error:

它允许您尝试操作,并且在抛出异常的情况下,您可以优雅地处理它,而不是冒泡并以丑陋且通常无法恢复的错误形式暴露给用户:

try
{
    int result = 10 / 0;
}
catch(ArithmeticException ae)
{
    System.out.println("You can not divide by zero");
}

// operation continues here without crashing

回答by Humphrey Bogart

tryis often used alongside catchfor code that could go wrong at runtime, an event know as throwing an exception. It is used to instruct the machine to try to run the code, and catch any exceptions that occur.

try通常与catch可能在运行时出错的代码一起使用,一个称为抛出异常的事件。它用于指示机器尝试运行代码,并捕获发生的任何异常。

So, for example, if you were to request to open a file that didn't exist the language warns you that something has gone wrong (namely that it was passed some erroneousinput), and allows you to account for it happening by enclosing it in a try..catchblock.

因此,例如,如果您要请求打开一个不存在的文件,该语言会警告您出现问题(即传递了一些错误的输入),并允许您通过将其封闭来解释它的发生在一个try..catch街区。

File file = null;

try {
    // Attempt to create a file "foo" in the current directory.
    file = File("foo");
    file.createNewFile();
} catch (IOException e) {
    // Alert the user an error has occured but has been averted.
    e.printStackTrace();
}

An optional finallyclause can be used after a try..catchblock to ensure certain clean-up (like closing a file) alwaystakes place:

finally可以在try..catch块之后使用可选子句以确保始终进行某些清理(如关闭文件):

File file = null;

try {
    // Attempt to create a file "foo" in the current directory.
    file = File("foo");
    file.createNewFile();
} catch (IOException e) {
    // Alert the user an error has occured but has been averted.
    e.printStackTrace();
} finally {
    // Close the file object, so as to free system resourses.
    file.close();
}

回答by sam

Exception handling

异常处理

回答by Jaime Garcia

You're talking about a "try/catch" block. It's used to capture exceptions that may occur within the block of code inside the "try/catch". Exceptions will be handled in the "catch" statement.

你说的是“try/catch”块。它用于捕获“try/catch”内的代码块中可能发生的异常。异常将在“catch”语句中处理。

回答by Justin Ethier

It allows you to define an exception handler for a block of code. This code will be executed and if any "exception" (null pointer reference, I/O error, etc) occurs, the appropriate handler will be called, if one is defined.

它允许您为代码块定义异常处理程序。将执行此代码,并且如果发生任何“异常”(空指针引用、I/O 错误等),则将调用适当的处理程序(如果已定义)。

For more information, see Exception Handlingon wikipedia.

有关更多信息,请参阅维基百科上的异常处理