日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java 批量处理 示例_Java中异常处理的示例

發布時間:2023/12/1 java 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 批量处理 示例_Java中异常处理的示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

java 批量處理 示例

Here, we will analyse some exception handling codes, to better understand the concepts.

在這里,我們將分析一些異常處理代碼 ,以更好地理解這些概念。

Try to find the errors in the following code, if any

嘗試在以下代碼中查找錯誤(如果有)

Code 1:

代碼1:

public class prog {public static void main(String arg[]) {try {int a = 10, b = 0;int c = a / b;} catch (RuntimeException e) {System.out.println(e.getMessage());} catch (ArithmeticException e) {System.out.println(e.getMessage());}}}

Output

輸出量

/prog.java:8: error: exception ArithmeticException has already been caught} catch (ArithmeticException e) {^ 1 error

Explanation:

說明:

When using multiple catch blocks, we have to make sure that the exceptions are caught in the reverse order of inheritance of the exceptions. This means that most specific exceptions must be caught before the most general exceptions. ArithmeticException must be caught before the RuntimeException.

當使用多個catch塊時,我們必須確保以與異常繼承相反的順序捕獲異常。 這意味著必須在最一般的異常之前捕獲最具體的異常。 必須在RuntimeException之前捕獲ArithmeticException



Code 2:

代碼2:

public class prog {public static void main(String arg[]) {try {throw new Integer(25);} catch (Exception e) {System.out.println(e.getMessage());}} }

Output

輸出量

/prog.java:4: error: incompatible types: Integer cannot be converted to Throwablethrow new Integer(25);^ Note: /prog.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. 1 error

Explanation:

說明:

We can only throw objects of Throwable class or classes that inherit this class. Integer class does not extend the Throwable class and thus, we cannot throw objects of Integer class. Similarly the statement "throw 25" is erroneous. Unlike C++, where we can throw objects, in Java we can only throw those objects that inherit the Throwable class.

我們只能拋出Throwable類或繼承該類的類的對象。 Integer類不會擴展Throwable類,因此,我們不能拋出Integer類的對象。 類似地, “ throw 25”的陳述是錯誤的 。 與C ++可以拋出對象不同,在Java中,我們只能拋出那些繼承Throwable類的對象。



Code 3:

代碼3:

public class prog {public static void main(String arg[]) {err ob1 = new err();ob1.func();} }class err {void func() {try {System.out.println("Inside try");} finally {System.out.println("Inside finally");}} }

Output

輸出量

Inside try Inside finally

Explanation:

說明:

It is not necessary that we attach a catch block to a try block. Try statement can be associated with a finally statement directly.

不必將catch塊附加到try塊。 try語句可以直接與finally語句關聯。



Code 4:

代碼4:

import java.io.*;public class prog {public static void main(String arg[]) {err ob1 = new err();ob1.func();} }class err {void func() throws IOException {try {System.out.println("Inside try");} catch (Exception e) {System.out.println("Inside catch");} finally {System.out.println("Inside finally");}} }

Output

輸出量

/prog.java:6: error: unreported exception IOException; must be caught or declared to be thrownob1.func();^ 1 error

Explanation:

說明:

If a function is said to throw any checked exception, then that checked exception must be caught by the caller. The statement 'void func() throws IOException' clearly states that the function can throw an IOException that must be handled by the caller. The error can be corrected by enclosing the 'ob1.func()' statement in a try-catch block.

如果說一個函數拋出了任何已檢查的異常,則調用者必須捕獲該已檢查的異常。 聲明“ void func()throws IOException ”清楚地表明該函數可以拋出IOException,必須由調用者處理。 可以通過在try-catch塊中包含' ob1.func() '語句來糾正該錯誤。



Code 5:

代碼5:

import java.io.*;public class prog {public static void main(String arg[]) {err ob1 = new err();ob1.func();} }class err {void func() throws RuntimeException {try {System.out.println("Inside try");try {int[] a = new int[10];int c = 10;a[c] = 0;}} catch (Exception e) {System.out.println("Inside catch");}} }

Output

輸出量

/prog.java:14: error: 'try' without 'catch', 'finally' or resource declarationstry {^ 1 error

Explanation:

說明:

Every try block must have an associated catch or finally block. In the code, the inner try block does not have an associated catch or finally block.

每個try塊必須具有關聯的catch或finally塊。 在代碼中,內部try塊沒有關聯的catch或finally塊。



翻譯自: https://www.includehelp.com/java/examples-on-exceptional-handing-in-java.aspx

java 批量處理 示例

總結

以上是生活随笔為你收集整理的java 批量处理 示例_Java中异常处理的示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。