以下文章提供了 Java 中的 Final 的概述。最后是与 try 和 catch 一起使用的代码块。最后包含无论是否发生异常都必须执行的代码块。无论 try 块中是否发生错误,finally 块中编写的语句始终都会执行。最后,该块非常适合关闭文件或数据库连接,确保您不会因打开文件而出现任何内存错误,也不会因打开连接或最大连接错误而出现数据库错误。它还确保代码中发生的任何错误都会得到妥善处理。
广告 该类别中的热门课程 财务建模和估值 - 专业化 | 51 课程系列 | 30 次模拟测试开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
语法:
try { //block of code that may cause an exception }
catch { //Handling the error occurred in try block }
finally { //code that should always execute }
这里我们将抛出一个错误或编写一个错误的代码,这将导致错误并最终阻止执行。
代码:
class ExampleFinally { public static void main(String args[]) { try{ int result = 1/0; System.out.println(result); catch(ArithmeticException e){ System.out.println("Divide by Zero Error"); } /* Finally block will always execute * even when we have created an error Block */ finally{ System.out.println("Gracefully Handling Error in Finally"); } System.out.println("Execution complete out of Try Catch Finally block"); } }
输出:
说明:
在上面的程序中,我们已经将一个数字除以零。
在 try catch finally、block 之后,我们在打印的所有内容之外编写了一段代码。
另一个例子,我们将看到 try-catch-finally 块内不会发生异常,并看看会发生什么。
代码:
class ExampleFinally { public static void main(String args[]) { try { int result = 100/10; System.out.println(result); System.out.println("try code block"); } catch (Exception e) { System.out.println("catch code block"); } finally { System.out.println("finally code block"); } } }
输出:
说明:
在上面的程序中,我们没有编写任何可能导致错误的代码。代码在 try 块内成功执行,但是您仍然可以看到 finally 块被执行并在里面打印了消息。
打开文件进行读取或写入需要打开一个文件,然后缓冲流,并且我们应该确保关闭打开的文件,这样我们就不会出现任何文件处理、IO 或磁盘错误。
代码:
import java.io.*; public class ReadingFromFile { public static void main(String[] args)throws Exception { FileReader fr = null; try { fr = new FileReader("/Users/cdp/Documents/testing/python virtual/java/myfile.txt");<> System.out.println("Opening the file"); BufferedReader br = new BufferedReader(fr); String line; while ((line = br.readLine()) != null) System.out.println(line); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (fr != null) { try { System.out.println("Closing the file "); fr.close(); } catch (IOException e) { System.out.println("unrecoverable Error occurred"); e.printStackTrace(); } } System.out.println("Exiting Finally block"); } } }
输出(文件不存在):
输出(文件存在):
在上面的示例中,我们尝试打开一个文件并将其从文件路径读入缓冲区。如果文件存在并且我们能够读取该文件,则不会抛出任何错误,并且如果文件缓冲区不为空,则文件缓冲区将在finally块中关闭。即使读取文件时出现错误,比如说由于某些权限,那么在finally块中文件也会被关闭。
到目前为止,我们已经了解了finally 块何时以及如何被执行。
但是在某些情况下,finally 块可能不会被执行。
Example:
import java.io.*; public class ReadingFromFile { public static void main(String[] args)throws Exception { FileReader fr = null; try { fr = new FileReader("/Users/cdp/Documents/testing/python virtual/java/myfile.txt"); System.out.println("Opening the file"); BufferedReader br = new BufferedReader(fr); String line; while ((line = br.readLine()) != null) System.out.println(line); System.exit(0); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (fr != null) { try { System.out.println("Closing the file "); fr.close(); } catch (IOException e) { System.out.println("unrecoverable Error occurred"); e.printStackTrace(); } } System.out.println("Exiting Finally block"); } } }
Output:
In the above example, we have used System.exit in the try block after reading the file, and it gets executed. If the System.exit gets executed without any exception, then there won’t be any control transfer to the finally block. However, in the case of an exception occuring before the System.exit, then finally block would surely get executed.
In the conclusion we can reach, it can finally play a very important role in gracefully exiting the program in case of errors. Finally, the block is important when you open a connection or buffered something, and it’s always important to close the connection or file opened. Finally, the block would even execute if there is an error or no error in the try block code. Finally, blocks are not mandatory but are useful in some situations.
以上是终于在Java中的详细内容。更多信息请关注PHP中文网其他相关文章!