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 批量處理 示例