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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/378284.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/378284.shtml
英文地址,請注明出處:http://en.pswp.cn/news/378284.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

hdu 1465 不容易系列之一

http://acm.hdu.edu.cn/showproblem.php?pid1465 今天立神和我們講了錯排,才知道錯排原來很簡單,從第n個推起: 當n個編號元素放在n個編號位置,元素編號與位置編號各不對應的方法數用M(n)表示,那么M(n-1)就表示n-1個編號元素放在n-1個編號位置…

第十四章 網絡編程

第十四章 網絡編程 本章首先概述Python標準庫中的一些網絡模塊。然后討論SocketServer和相關的類,并介紹同時處理多個連接的各種方法。最后,簡單地說一說Twisted,這是一個使用Python編寫網絡程序的框架,功能豐富而成熟。 幾個網…

c語言輸出11258循環,c/c++內存機制(一)(轉)

一:C語言中的內存機制在C語言中,內存主要分為如下5個存儲區:(1)棧(Stack):位于函數內的局部變量(包括函數實參),由編譯器負責分配釋放,函數結束,棧變量失效。(2)堆(Heap):由程序員用…

【神經網絡八股擴展】:數據增強

課程來源:人工智能實踐:Tensorflow筆記2 文章目錄前言TensorFlow2數據增強函數數據增強網絡八股代碼:總結前言 本講目標:數據增強,增大數據量 關于我們為何要使用數據增強以及常用的幾種數據增強的手法,可以看看下面的文章&#…

C++:從C繼承的標準庫

C從C繼承了的標準庫 &#xff0c; 這就意味著 C 中 可以使用的標準庫函數 在C 中都可以使用 &#xff0c; 但是需要注意的是 &#xff0c; 這些標準庫函數在C中不再以 <xxx.h> 命名 &#xff0c; 而是變成了 <cxxx> 。 例如 &#xff1a; 在C中操作字符串的…

分享WCF聊天程序--WCFChat

無意中在一個國外的站點下到了一個利用WCF實現聊天的程序&#xff0c;作者是&#xff1a;Nikola Paljetak。研究了一下&#xff0c;自己做了測試和部分修改&#xff0c;感覺還不錯&#xff0c;分享給大家。先來看下運行效果&#xff1a;開啟服務&#xff1a;客戶端程序&#xf…

c# uri.host_C#| 具有示例的Uri.Equality()運算符

c# uri.hostUri.Equality()運算符 (Uri.Equality() Operator) Uri.Equality() Operator is overloaded which is used to compare two Uri objects. It returns true if two Uri objects contain the same Uri otherwise it returns false. Uri.Equality()運算符已重載&#xf…

第六章至第九章的單元測試

1,?助劑與纖維作用力大于纖維分子之間的作用力,則該助劑最好用作() 纖維增塑膨化劑。 2,助劑擴散速率快,優先占領纖維上的染座,但助劑與纖維之間作用力小于染料與纖維之間作用力,該助劑可以作為() 勻染劑。 3,助劑占領纖維上的染座,但助劑與纖維之間作用力大于染…

【神經網絡擴展】:斷點續訓和參數提取

課程來源&#xff1a;人工智能實踐:Tensorflow筆記2 文章目錄前言斷點續訓主要步驟參數提取主要步驟總結前言 本講目標:斷點續訓&#xff0c;存取最優模型&#xff1b;保存可訓練參數至文本 斷點續訓主要步驟 讀取模型&#xff1a; 先定義出存放模型的路徑和文件名&#xff0…

開發DBA(APPLICATION DBA)的重要性

開發DBA是干什么的&#xff1f; 1. 審核開發人員寫的SQL&#xff0c;并且糾正存在性能問題的SQL ---非常重要 2. 編寫復雜業務邏輯SQL&#xff0c;因為復雜業務邏輯SQL開發人員寫出的SQL基本上都是有性能問題的&#xff0c;與其讓開發人員寫&#xff0c;不如DBA自己寫。---非常…

javascript和var之間的區別?

You can define your variables in JavaScript using two keywords - the let keyword and the var keyword. The var keyword is the oldest way of defining and declaring variables in JavaScript whereas the let is fairly new and was introduced by ES15. 您可以使用兩…

小米手環6NFC安裝太空人表盤

以前看我室友峰哥、班長都有手環&#xff0c;一直想買個手環&#xff0c;不舍得&#xff0c;然后今年除夕的時候降價&#xff0c;一狠心&#xff0c;入手了&#xff0c;配上除夕的打年獸活動還有看春晚京東敲鼓領的紅包和這幾年攢下來的京東豆豆&#xff0c;原價279的小米手環6…

計算機二級c語言題庫縮印,計算機二級C語言上機題庫(可縮印做考試小抄資料)...

小抄,答案,形成性考核冊,形成性考核冊答案,參考答案,小抄資料,考試資料,考試筆記第一套1.程序填空程序通過定義學生結構體數組&#xff0c;存儲了若干個學生的學號、姓名和三門課的成績。函數fun 的功能是將存放學生數據的結構體數組&#xff0c;按照姓名的字典序(從小到大排序…

為什么兩層3*3卷積核效果比1層5*5卷積核效果要好?

目錄1、感受野2、2層3 * 3卷積與1層5 * 5卷積3、2層3 * 3卷積與1層5 * 5卷積的計算量比較4、2層3 * 3卷積與1層5 * 5卷積的非線性比較5、2層3 * 3卷積與1層5 * 5卷積的參數量比較1、感受野 感受野&#xff1a;卷積神經網絡各輸出特征像素點&#xff0c;在原始圖片映射區域大小。…

算法正確性和復雜度分析

算法正確性——循環不變式 算法復雜度的計算 方法一 代換法 —局部代換 這里直接對n變量進行代換 —替換成對數或者指數的情形 n 2^m —整體代換 這里直接對遞推項進行代換 —替換成內部遞推下標的形式 T(2^n) S(n) 方法二 遞歸樹法 —用實例說明 —分析每一層的內容 —除了…

第十五章 Python和Web

第十五章 Python和Web 本章討論Python Web編程的一些方面。 三個重要的主題&#xff1a;屏幕抓取、CGI和mod_python。 屏幕抓取 屏幕抓取是通過程序下載網頁并從中提取信息的過程。 下載數據并對其進行分析。 從Python Job Board&#xff08;http://python.org/jobs&#x…

array_chunk_PHP array_chunk()函數與示例

array_chunkPHP array_chunk()函數 (PHP array_chunk() Function) array_chunk() function is an array function, it is used to split a given array in number of array (chunks of arrays). array_chunk()函數是一個數組函數&#xff0c;用于將給定數組拆分為多個數組(數組…

raise

raise - Change a windows position in the stacking order button .b -text "Hi there!"pack [frame .f -background blue]pack [label .f.l1 -text "This is above"]pack .b -in .fpack [label .f.l2 -text "This is below"]raise .b轉載于:ht…

c語言輸出最大素數,for語句計算輸出10000以內最大素數怎么搞最簡單??各位大神們...

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓#include #include int* pt NULL; // primes_tableint pt_size 0; // primes_table 數量大小int init_primes_table(void){FILE* pFile;pFile fopen("primes_table.bin", "rb");if (pFile NULL) {fputs(&q…

【數據結構基礎筆記】【圖】

代碼參考《妙趣橫生的算法.C語言實現》 文章目錄前言1、圖的概念2、圖的存儲形式1、鄰接矩陣&#xff1a;2、鄰接表3、代碼定義鄰接表3、圖的創建4、深度優先搜索DFS5、廣度優先搜索BFS6、實例分析前言 本章總結&#xff1a;圖的概念、圖的存儲形式、鄰接表定義、圖的創建、圖…