CodeGym /Kursus Jawa /Sintaksis Jawa /Jinis pengecualian

Jinis pengecualian

Sintaksis Jawa
tingkat , wulangan
kasedhiya
Jinis pengecualian

"Aku pengin ngomong babagan siji maneh dina iki. Ing Jawa, kabeh pengecualian dipérang dadi rong jinis: dicenthang lan ora dicenthang (sing kudu dicekel lan sing ora kudu dicekel). Secara default, kabeh pengecualian kudu kejiret."

"Apa sampeyan bisa kanthi sengaja mbuwang pengecualian ing kode sampeyan?"

"Sampeyan bisa mbuwang sembarang pangecualian ing kode dhewe. Sampeyan bisa malah nulis pangecualian dhewe. Nanging kita bakal pirembagan bab iku mengko. Sapunika, ayo kang musataken ing istiméwa di buwang dening Mesin Jawa."

"Oke."

"Yen ClassNotFoundException utawa FileNotFoundException dibuwang (kedadeyan) ing cara, pangembang kudu nunjukake ing deklarasi metode. Iki minangka pengecualian sing dicenthang . Iki biasane katon: "

Conto pangecualian sing dicenthang
public static void method1() throws ClassNotFoundException, FileNotFoundException

public static void main() throws IOException
public static void main() // Doesn't throw any exceptions

"Dadi kita mung nulis 'uncalan' diikuti karo dhaptar pengecualian sing diwatesi koma, ta?"

"Ya. Nanging ana liyane. Kanggo program kanggo ngumpulake, cara sing nelpon method1ing conto ing ngisor iki kudu nindakake salah siji saka rong perkara: salah siji nyekel pangecualian iki utawa rethrow wong (menyang panelpon) , nuduhake pangecualian rethrown ing deklarasi. "

"Maneh. Yen cara utama sampeyan kudu nelpon metode sing deklarasi ngemot ' mbalang FileNotFoundException , ...', mula sampeyan kudu nindakake salah siji saka rong perkara:

1) Nyekel FileNotFoundException , ...

Sampeyan kudu mbungkus kode sing nelpon cara sing ora aman ing blok nyoba-nyekel .

2) Aja nyekel FileNotFoundException , ...

Sampeyan kudu nambahake pangecualian kasebut menyang dhaptar uncalan metode utama sampeyan ."

"Apa sampeyan bisa menehi conto?"

"Coba delok iki:"

Conto pangecualian sing dicenthang
public static void main(String[] args)
{
    method1();
}

public static void method1() throws FileNotFoundException, ClassNotFoundException
{
    //Throws FileNotFoundException if the file doesn't exist
    FileInputStream fis = new FileInputStream("C2:\badFileName.txt");
}

"Kode ing conto iki ora bakal ngumpulake, amarga cara utama nelpon method1 () , kang mbalang pangecualian sing kudu kejiret."

"Kanggo nggawe kompilasi, kita kudu nambah penanganan pangecualian menyang metode utama. Sampeyan bisa nindakake iki kanthi salah siji saka rong cara: "

Opsi 1: Kita mung mbaleni pangecualian (kanggo panelpon):
public static void main(String[] args)  throws FileNotFoundException, ClassNotFoundException 
{
    method1();
}

public static void method1() throws FileNotFoundException, ClassNotFoundException
{
    //Throws FileNotFoundException if the file doesn't exist
    FileInputStream fis = new FileInputStream("C2:\badFileName.txt");
}

"Lan kene kita nyekel karo nyoba-nyekel :"

Pilihan 2: Nyekel pengecualian:
public static void main(String[] args)
{
    try
    {
        method1();
    }
    catch(Exception e)
    {
    }
}

public static void method1() throws FileNotFoundException, ClassNotFoundException
{
    //Throws FileNotFoundException if the file doesn't exist
    FileInputStream fis = new FileInputStream("C2:\badFileName.txt");
}

"Wis wiwit dadi luwih cetha."

"Deleng conto ing ngisor iki. Iku bakal mbantu sampeyan ngerti liyane."

Tinimbang nangani pangecualian, kita bali menyang cara sing luwih adoh saka tumpukan telpon sing ngerti carane nangani:
public static void method2() throws FileNotFoundException, ClassNotFoundException
{
    method1();
}
Nangani siji pengecualian lan uncalan liyane:
public static void method3() throws ClassNotFoundException
{
   try
    {
        method1();
    }
    catch (FileNotFoundException e)
    {
        System.out.println("FileNotFoundException has been caught.");
    }
}
Nyekel loro pengecualian, ora mbuwang:
public static void method4()
{
    try
    {
        method1();
    }
    catch (FileNotFoundException e)
    {
        System.out.println("FileNotFoundException has been caught.");
    }
    catch (ClassNotFoundException e)
    {
        System.out.println("ClassNotFoundException has been caught.");
    }
}

"Ana siji liyane jinis pangecualian, RuntimeException lan kelas sing oleh warisan. Sampeyan ora perlu kanggo nyekel wong-wong mau. Iki minangka pangecualian sing ora dicenthang. Padha dianggep angel kanggo prédhiksi. Sampeyan bisa ngatasi kanthi cara sing padha, nanging sampeyan ora perlu nuduhake dheweke ing klausa uncalan ."

Komentar
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION