异常是在 Java 中运行代码时遇到任何错误时抛出的异常。 java中的RuntimeException被称为Java编程语言中所有异常的父类,当程序或应用程序执行时,一旦发生异常,就会崩溃或崩溃。但与其他异常相比,这些异常是不同的,无法像其他异常那样通过在代码中指定来捕获。
按照Object的顺序属于Exception的父类->可投掷 ->异常 -> RuntimeException。因此,它可以被称为运行 JVM(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 () 方法获取,并且当允许空值时,表明其原因未知。
我们为避免此类异常而采取的方法称为异常处理。这是开发人员在编码时应牢记的最基本的事情之一,因为如果发生异常并且无法处理相同的异常,则整个代码将毫无用处。
我们使用某些称为 throw 和 throw 的子句来处理 Java 中的检查异常。运行时异常通常是由于输入错误而发生,并导致 ArrayIndexOutOfBoundsException、IllegalArgumentException、NumberFormatException 或 NullPointerException 等异常。在代码中包含这些错误,处理不会做出任何改变,但它可以用于文档请求作为一个很好的实践。
我们可以自定义运行时异常,如下所示:
public class AuthenticateUser extends RuntimeException { public AuthenticateUser (String msg) { super (msg); } }
以下是4种主要运行时异常的示例:
当我们请求无效或不可用的数组索引值时,就会发生这种情况。
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.
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.
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.
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.
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 –
以上是Java运行时异常的详细内容。更多信息请关注PHP中文网其他相关文章!