ホームページ  >  記事  >  Java  >  Java算術例外

Java算術例外

WBOY
WBOYオリジナル
2024-08-30 16:13:38760ブラウズ

Java 算術例外は、実行時にコード内で間違った算術演算または数学的演算が発生したときにスローされる、一種の未チェック エラーまたはコードの異常な結果です。例外とも呼ばれる実行時の問題は、分母が整数 0 の場合に発生し、JVM が結果を評価できないため、プログラムの実行が終了し、例外が発生します。例外が発生した時点でプログラムは終了しますが、それより前のコードが実行され、結果が表示されます。

Java 算術例外の基本クラスは lang.ArithmeticException であり、java.lang.RuntimeException に属します。

広告 このカテゴリーの人気コース JAVA マスタリー - スペシャライゼーション | 78 コース シリーズ | 15 回の模擬テスト

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

Java の ArithmeticException の構造

基本クラス ArithmeticException の構造:

Java算術例外

ArithmeticException のコンストラクター

1. ArithmeticException(): パラメーターが渡されないか、詳細メッセージが渡されない算術例外を定義します。

2. ArithmeticException(String s): 1 つのパラメータを渡して ArithmeticException を定義します。

s: s は詳細メッセージです

Java で ArithmeticException はどのように動作しますか?

Java ArithmeticException が発生する可能性のある 2 つの状況を以下に示します。

  • 定義されていない数値と整数をゼロで除算します。
  • Big Decimal による終端のない長い 10 進数。

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 ゼロ: 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. Big Decimal による終端のない Long Decimal 数値

Java には、最大精度桁数までの 10 進数を表す 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 コードでは、big 10 進クラスは除算出力をどのように処理すればよいのかわからないため、出力コンソールに算術例外をスローするか表示します。

「非終了小数展開、正確な表現がありません。」という詳細メッセージを含む例外がスローされます。

上記のビッグ 10 進数クラスで考えられる方法の 1 つは、ビッグ 10 進数から必要な小数点以下の桁数を指定し、値を明確な小数桁数に制限することです。たとえば、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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。