首頁  >  文章  >  Java  >  Java 算術異常

Java 算術異常

WBOY
WBOY原創
2024-08-30 16:13:38760瀏覽

Java算術異常是一種未經檢查的錯誤或程式碼異常結果,當程式碼在執行時發生錯誤的算術或數學運算時拋出。運行時問題,也稱為異常,是指當分母為整數 0 時,JVM 無法計算結果,從而終止程式的執行,並引發異常。引發異常的點程序終止,但執行先前的程式碼,並顯示結果。

java算術異常的基底類別是lang.ArithmeticException,它屬於java.lang.RuntimeException。

廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

Java中ArithmeticException的結構

基類ArithmeticException的結構:

Java 算術異常

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);
}
}

輸出:

Java 算術異常

  • 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);
}
}

輸出:

Java 算術異常

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 算術異常

在上面寫的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 算術異常

輸出控制台將結果顯示為具有第 7

位小數點值的數字,這表示舍入效果很好。

如何避免或處理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:

Java 算術異常

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:

  1. 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.
  2. 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.
  3. If the try block does not throw the exception, the catch block is simply overlooked.
  4. 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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn