首页  >  文章  >  Java  >  Java 算术异常

Java 算术异常

WBOY
WBOY原创
2024-08-30 16:13:38763浏览

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代码中,由于大十进制类不知道如何处理除法输出,因此它在输出控制台中抛出或显示算术异常。

它抛出一个异常,并带有详细消息“非终止十进制扩展,没有精确的表示。”

上述大十进制类的一种可能的出路是从一个大十进制数中陈述我们需要的小数位数,然后将该值限制为一定的小数位数。例如,通过将数字四舍五入到第 7 位小数精度值,应将 c 限制为小数点后 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