Java算術異常是一種未經檢查的錯誤或程式碼異常結果,當程式碼在執行時發生錯誤的算術或數學運算時拋出。運行時問題,也稱為異常,是指當分母為整數 0 時,JVM 無法計算結果,從而終止程式的執行,並引發異常。引發異常的點程序終止,但執行先前的程式碼,並顯示結果。
java算術異常的基底類別是lang.ArithmeticException,它屬於java.lang.RuntimeException。
廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
Java中ArithmeticException的結構
基類ArithmeticException的結構:
ArithmeticException 的建構子
1。 ArithmeticException(): 定義一個沒有傳遞參數或沒有任何詳細訊息的算術異常。
2。 ArithmeticException(String s): 定義一個傳遞一個參數的 ArithmeticException。
s: s 為詳細訊息
ArithmeticException 在 Java 中如何運作?
以下是兩種可能導致 Java ArithmeticException 的情況:
- 數字除以未定義的零和整數。
- Big Decimal 的非終止長十進制數。
1.數字除以整數零
當我們嘗試將數字除以零時,Java 中會拋出算術異常。下面是java程式碼來說明操作:
範例#1
代碼:
package com.java.exception; public class ArithmeticException { void division(int a,int b) { int c=a/b; System.out.println("Division has been successfully done"); System.out.println("Value after division: "+c); } public static void main(String[] args) { ArithmeticException ex=new ArithmeticException(); ex.division(10,0); } }
輸出:
- lang.ArithmeticException:除法時java語言拋出的異常
- / by 0:這是產生 ArithmeticException 實例時給予類別 ArithmeticException 的詳細訊息。
由於我們將 10 除以 0,其中 0 是整數且未定義,因此會拋出上述算術異常。
範例#2
代碼:
//package com.java.exception; public class ArithmeticException { void division(int a,int b) { int c=a/b; System.out.println("Division of a number is successful"); System.out.println("Output of division: "+c); } public static void main(String[] args) { ArithmeticException ex=new ArithmeticException(); ex.division(10,5); } }
輸出:
2.大十進制非終止長十進制數
Java 有一個 BigDecimal 類,它表示十進制數,最高可達大量精度數字。此類 java 還具有一些基本資料類型中不可用的功能集,例如整數、雙精度數和浮點數。這些功能提供對小數的四捨五入
以下是示範程式碼:
範例#1
代碼:
//package com.java.exception; import java.math.BigDecimal; public class ArithmeticException { public static void main(String[] args) { BigDecimal a=new BigDecimal(1); BigDecimal b=new BigDecimal(6); a=a.divide(b); System.out.println(a.toString()); } }
輸出:
在上面寫的java程式碼中,由於大十進位類別不知道如何處理除法輸出,因此它在輸出控制台中拋出或顯示算術異常。
它拋出一個異常,並帶有詳細訊息「非終止十進制擴展,沒有精確的表示。」
上述大十進制類別的一個可能的出路是從一個大十進制數中陳述我們需要的小數位數,然後將該值限制為一定的小數位數。例如,c 應透過將數字四捨五入到第 7 位小數精度值來限制為小數點後 7 位。
範例#2代碼:
//package co.java.exception; import java.math.BigDecimal; public class ArithmeticException { public static void main(String[] args) { BigDecimal a=new BigDecimal(1); BigDecimal b=new BigDecimal(6); a=a.divide(b,7,BigDecimal.ROUND_DOWN);// limit of decimal place System.out.println(a.toString()); } }
輸出:
位小數點值的數字,這表示舍入效果很好。
如何避免或處理Java ArithmeticException?處理java虛擬機器拋出的異常稱為異常處理。異常處理的好處是程式碼的執行不會停止。
異常是透過使用try和catch的組合來處理的。 try/catch 區塊被放置在可能產生異常的程式碼中。在 try/catch 區塊內編寫的程式碼稱為受保護程式碼。
文法:
try { set of statements//protected code } catch (exceptionname except) { // Catch set of statements---can contain single catch or multiple. }
ArithmeticException Handling using try & Catch Blocks
- Write the statements that can throw ArithmeticException with try and catch blocks.
- When an exception occurs, the execution falls to the catch block from an exception’s point of occurrence. It executes the catch block statement and continues with the statement present after the try and catch blocks. Below is the example:
Code:
public class ExceptionHandled { public static void main(String args[]) { int x =100, y = 0; int z; System.out.println("Hello world"); try { z = x/y; System.out.println(z); } catch(ArithmeticException except) { System.out.println("Avoid dividing by integer 0" + except ); } System.out.println("Hello class"); System.out.println("Hello there"); } }
Output:
Hello class and Hello, there are also printed on the output console apart from Hello world. This is the outcome of the exception handling mechanism.
Explanation:
- Try, and catchblocks are exception handling keywords in Java used completely used to handle the exception or unchecked error raised in the code without halting the execution of the code.
- Detect the trouble creating statements and place them in the try block. When the try block throws the exception, the catch block handles that exception, and the execution of the program goes further to the last statement.
- If the try block does not throw the exception, the catch block is simply overlooked.
- Run a suitable exception handler or statements in the catch block to handle or detect the exception thrown by the try block successfully.
Conclusion
This article has learned about java arithmetic exception and how to handle the exception under try and catch block. An arithmetic exception occurs most of the time because of the division of a number by integer 0. In this article, I have used a single catch for a single try, but we can also use multiple catch for a single try.
以上是Java 算術異常的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA

本文解釋了用於構建分佈式應用程序的Java的遠程方法調用(RMI)。 它詳細介紹了接口定義,實現,註冊表設置和客戶端調用,以解決網絡問題和安全性等挑戰。

本文詳細介紹了用於網絡通信的Java的套接字API,涵蓋了客戶服務器設置,數據處理和關鍵考慮因素,例如資源管理,錯誤處理和安全性。 它還探索了性能優化技術,我

本文詳細介紹了創建自定義Java網絡協議。 它涵蓋協議定義(數據結構,框架,錯誤處理,版本控制),實現(使用插座),數據序列化和最佳實踐(效率,安全性,維護


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SublimeText3 Linux新版
SublimeText3 Linux最新版

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3漢化版
中文版,非常好用

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。