Java运行时异常
异常是在 Java 中运行代码时遇到任何错误时抛出的异常。 java中的RuntimeException被称为Java编程语言中所有异常的父类,当程序或应用程序执行时,一旦发生异常,就会崩溃或崩溃。但与其他异常相比,这些异常是不同的,无法像其他异常那样通过在代码中指定来捕获。
Java 中 RuntimeException 的工作原理
按照Object的顺序属于Exception的父类->可投掷 ->异常 -> RuntimeException。因此,它可以被称为运行 JVM(Java 虚拟机)常规操作时可能抛出的所有异常的超类。这个 RuntimeException 及其子类属于一类称为“未经检查的异常”的异常。这些不能也不需要在构造函数或方法的子句中指定。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
Java 中 RuntimeException 的构造函数
下面是 RuntimeException 的构造函数:
1。 RuntimeException (): 这会向我们抛出新的运行时异常,其详细消息为 null。
语法:
public RuntimeException()
这里的cause不会被初始化,可以通过调用类Throwable.initCause(java.lang.Throwable)来完成。
2。 RuntimeException (String msg): 这也会抛出一个新的运行时异常,但具有我们在 Java 代码中提供的定义的详细消息。
语法:
public RuntimeException (String msg)
和上面的函数一样,默认不会初始化cause,同样可以通过调用Throwable.initCause(java.lang.Throwable)来完成。这里的msg是详细消息,将保存该消息以便稍后通过Throwable.getMessage()方法检索。
3。 RuntimeException (String msg, Throwable Cause): 这会抛出一个新的运行时异常,并带有定义的错误消息及其原因。
语法:
public RuntimeException (String message, Throwable cause)
请注意,此处的消息不会自动包含,必须显式指定。这里,原因是从 Throwable.getCause() 函数中获取的,这里允许为空值,表示其原因不存在或未知。
4。 RuntimeException (String msg, Throwable Cause, booleanenableSupp, booleanwritableStack): 这给出了一个新的运行时异常,其中详细描述了错误消息,其具体原因,enableSupp表示其抑制是否已启用或禁用,writableStack是其堆栈跟踪(如果启用或禁用)。
语法:
protected RuntimeException (String message, Throwable cause, booleanenableSuppression, booleanwritableStackTrace)
这会给出一个新的运行时异常,其中包含已定义的原因和指定的详细消息、其原因、是否启用或禁用抑制以及是否启用了可写堆栈跟踪。这里的message是我们要显示的具体消息,cause表示是否存在,enableSuppression表示是否允许抑制,writableStackTrace指定堆栈跟踪是否可写。
5。 RuntimeException (Throwable Cause): 这会抛出一个新的运行时异常,带有给定的原因和指定的条件的详细错误消息 (cause==null ? null : Cause.toString ()),它基本上具有类及其特定的引起消息。
语法:
public RuntimeException (Throwable cause)
原因被保留以供稍后通过 Throwable.getCause () 方法获取,并且当允许空值时,表明其原因未知。
如何避免 Java 中的 RuntimeException?
我们为避免此类异常而采取的方法称为异常处理。这是开发人员在编码时应牢记的最基本的事情之一,因为如果发生异常并且无法处理相同的异常,则整个代码将毫无用处。
我们使用某些称为 throw 和 throw 的子句来处理 Java 中的检查异常。运行时异常通常是由于输入错误而发生,并导致 ArrayIndexOutOfBoundsException、IllegalArgumentException、NumberFormatException 或 NullPointerException 等异常。在代码中包含这些错误,处理不会做出任何改变,但它可以用于文档请求作为一个很好的实践。
我们可以自定义运行时异常,如下所示:
public class AuthenticateUser extends RuntimeException { public AuthenticateUser (String msg) { super (msg); } }
示例
以下是4种主要运行时异常的示例:
示例 #1 – ArrayIndexOutOfBoundsException
当我们请求无效或不可用的数组索引值时,就会发生这种情况。
Code:
public class Main { public static void main (String[] args) { // Random array of numbers intip[] = {16, 17, 18, 19, 20}; for (inti=0; i<=ip.length; i++) System.out.println (ip[i]); } }
Output:
As seen in this example, in the input array has its index value from 0 to 4. But in this for loop, the length of the array retrieved will be 5, and when that is tried to access in the array, it will throw the ArrayIndexOutOfBoundsException during RunTime of the code.
Example #2 – IllegalArgumentException
The cause of this exception is when the argument format provided is invalid.
Code:
public class Main { inti; public void getMark (int score) { if (score < 0 || score > 100) throw new IllegalArgumentException (Integer.toString (score)); else i = score; } public static void main (String[] args) { Main t = new Main (); t.getMark (30); System.out.println (t.i); Main t1 = new Main (); t1.getMark (120); System.out.println (t1.i); } }
Output:
Here we know that the maximum value of a percentage value is 100. So when we pass the value as 101, we get the Illegal argument exception during run time.
Example #3 – NumberFormatException
This exception is usually thrown when a string is to be converted to a numeric value like either float or integer value, but the form of the string given as input is either illegal or inappropriate.
Code:
public class Main { // giving input string as null public static void main (String[] args) { inti = Integer.parseInt (null); } }
Output:
In this example, we are giving the input string to be parsed into an integer as null. Hence the number format exception is thrown.
Example #4 – NullPointerException
This exception occurs when a reference object that the variable is referring to is null.
Code:
public class Main { public static void main (String[] args) { Object reference = null; reference.toString (); } }
Output:
In this example, we are creating an object called reference having a null value. The same object is being called for an operation, and hence this error is thrown.
Conclusion: Runtime exceptions are thrown at runtime and hence difficult to be detected during compile time. They are difficult to handle, and the throws clause can only be used to define them but not catch them.
Recommended Article
This is a guide to Java RuntimeException. Here we discuss the Introduction and how to Avoid RuntimeException in Java, and it’s Working along with its examples. You can also go through our other suggested articles to learn more –
- Introduction to Heap Sort in Java
- Overriding in Java (Examples)
- Iterators in C# With Advantages and Disadvantages
- Top 10 Java Collection Interview Questions
以上是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)

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

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

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

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

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

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

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

PHP适用于Web开发和内容管理系统,Python适合数据科学、机器学习和自动化脚本。1.PHP在构建快速、可扩展的网站和应用程序方面表现出色,常用于WordPress等CMS。2.Python在数据科学和机器学习领域表现卓越,拥有丰富的库如NumPy和TensorFlow。
