public Throwable getCause()
Cause = java.lang.ArrayIndexOutOfBoundsException: 8
Apakah kepentingan kaedah getCause() dalam Java?
getCause() kaedah berasal dari kelas Throwable , kita boleh menggunakan kaedah ini untuk mengembalikan reason Exception atau return sebabnya tidak diketahui Kaedah getCause() tidak menerima sebarang parameter dan tidak membuang pengecualian. Ia mengembalikan punca yang disediakan oleh salah satu pembinanya atau ditentukan oleh pembentukan kaedah initCause() kelas Throwable .
Syntaxpublic Throwable getCause()
Salin selepas log masuk
Contohpublic Throwable getCause()
public class GetCauseMethodTest {
public static void main(String[] args) throws Exception {
try {
myException();
} catch(Exception e) {
System.out.println("Cause = " + e.getCause());
}
}
public static void myException() throws Exception {
int arr[] = {1, 3, 5};
try {
System.out.println(arr[8]);
} catch(ArrayIndexOutOfBoundsException aiobe) {
Exception e = new Exception();
throw(Exception); <strong>/</strong>/ throwing the exception to be caught by catch block in main()
e.initCause(aiobe); // supplies the cause to getCause()
}
}
}
Salin selepas log masuk
Outputpublic class GetCauseMethodTest { public static void main(String[] args) throws Exception { try { myException(); } catch(Exception e) { System.out.println("Cause = " + e.getCause()); } } public static void myException() throws Exception { int arr[] = {1, 3, 5}; try { System.out.println(arr[8]); } catch(ArrayIndexOutOfBoundsException aiobe) { Exception e = new Exception(); throw(Exception); <strong>/</strong>/ throwing the exception to be caught by catch block in main() e.initCause(aiobe); // supplies the cause to getCause() } } }
Cause = java.lang.ArrayIndexOutOfBoundsException: 8
Salin selepas log masuk
Cause = java.lang.ArrayIndexOutOfBoundsException: 8
Atas ialah kandungan terperinci Apakah kepentingan kaedah getCause() dalam Java?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Alat AI Hot

Undresser.AI Undress
Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover
Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool
Gambar buka pakaian secara percuma

Clothoff.io
Penyingkiran pakaian AI

AI Hentai Generator
Menjana ai hentai secara percuma.

Artikel Panas

Alat panas

Notepad++7.3.1
Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina
Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1
Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6
Alat pembangunan web visual

SublimeText3 versi Mac
Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Topik panas

