首頁  >  文章  >  Java  >  Java運行時異常

Java運行時異常

WBOY
WBOY原創
2024-08-30 16:14:06259瀏覽

異常是在 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:

Java運行時異常

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:

Java運行時異常

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:

Java運行時異常

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:

Java運行時異常

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 –

  1. Introduction to Heap Sort in Java
  2. Overriding in Java (Examples)
  3. Iterators in C# With Advantages and Disadvantages
  4. Top 10 Java Collection Interview Questions

以上是Java運行時異常的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:Java IO異常下一篇:Java IO異常