Java 算术异常
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代码中,由于大十进制类不知道如何处理除法输出,因此它在输出控制台中抛出或显示算术异常。
它抛出一个异常,并带有详细消息“非终止十进制扩展,没有精确的表示。”
上述大十进制类的一种可能的出路是从一个大十进制数中陈述我们需要的小数位数,然后将该值限制为一定的小数位数。例如,通过将数字四舍五入到第 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 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中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

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

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。

胶囊是一种三维几何图形,由一个圆柱体和两端各一个半球体组成。胶囊的体积可以通过将圆柱体的体积和两端半球体的体积相加来计算。本教程将讨论如何使用不同的方法在Java中计算给定胶囊的体积。 胶囊体积公式 胶囊体积的公式如下: 胶囊体积 = 圆柱体体积 两个半球体体积 其中, r: 半球体的半径。 h: 圆柱体的高度(不包括半球体)。 例子 1 输入 半径 = 5 单位 高度 = 10 单位 输出 体积 = 1570.8 立方单位 解释 使用公式计算体积: 体积 = π × r2 × h (4

PHP和Python各有优势,适合不同场景。1.PHP适用于web开发,提供内置web服务器和丰富函数库。2.Python适合数据科学和机器学习,语法简洁且有强大标准库。选择时应根据项目需求决定。

Java是热门编程语言,适合初学者和经验丰富的开发者学习。本教程从基础概念出发,逐步深入讲解高级主题。安装Java开发工具包后,可通过创建简单的“Hello,World!”程序实践编程。理解代码后,使用命令提示符编译并运行程序,控制台上将输出“Hello,World!”。学习Java开启了编程之旅,随着掌握程度加深,可创建更复杂的应用程序。

PHP成为许多网站首选技术栈的原因包括其易用性、强大社区支持和广泛应用。1)易于学习和使用,适合初学者。2)拥有庞大的开发者社区,资源丰富。3)广泛应用于WordPress、Drupal等平台。4)与Web服务器紧密集成,简化开发部署。
