Qhov Tseem Ceeb Qhov Sib Txawv - Tshawb Xyuas vs Tsis Tshawb Xyuas Qhov Kev Zam hauv Java
Ib qho kev zam yog qhov yuam kev runtime. Muaj ob hom kev zam hu ua checked and uncheck exceptions. Thaum ib qho kev kuaj xyuas tshwm sim, daim ntawv thov Java txuas nrog rau cov peev txheej sab nraud xws li cov ntaub ntawv, ntaus ntawv lossis database. Cov kev zam no raug tshuaj xyuas los ntawm lub tshuab compiler. Qee qhov piv txwv ntawm kev kuaj xyuas yog IO kev zam thiab FileNotFound kev zam. Thaum muaj qhov tsis tau txheeb xyuas tshwm sim, daim ntawv thov tsis txuas nrog ib qho kev pab sab nraud. Cov kev zam no tsis raug txheeb xyuas los ntawm lub compiler. Qee qhov piv txwv ntawm cov kev zam tsis tau txheeb xyuas yog Arithmetic Exception thiab ArrayOutOfBound Exception. Kab lus no tham txog qhov sib txawv ntawm kev kuaj xyuas thiab tsis tau kuaj xyuas hauv Java. Qhov sib txawv tseem ceeb ntawm kev kuaj xyuas thiab tsis tau txheeb xyuas qhov tshwj xeeb hauv Java yog qhov kev kuaj xyuas tshwj xeeb yog kuaj xyuas los ntawm tus sau thaum qhov tsis tau txheeb xyuas tsis raug kuaj xyuas los ntawm tus sau.
Dab tsi txheeb xyuas hauv Java?
Thaum muaj qhov kuaj pom tshwm sim, daim ntawv thov Java txuas nrog rau cov peev txheej sab nraud. Cov peev txheej no tuaj yeem yog cov cuab yeej xws li tshuab luam ntawv. Nws tuaj yeem yog cov ntaub ntawv lossis database. Yog li ntawd, cov kev zam no raug soj ntsuam los ntawm lub compiler. IO kev zam yog ib qho kev kuaj xyuas. Nws tshwm sim vim qhov yuam kev hauv lub cuab yeej. Thaum daim ntawv thov nkag mus rau cov ntaub ntawv uas tsis muaj nyob, ces nws yuav ua rau muaj kev zam FileNotFound. Ib daim ntawv thov tuaj yeem txuas nrog lub database xws li MySQL, Oracle thiab lwm yam los khaws cov ntaub ntawv. Yog hais tias qhov yuam kev tshwm sim muaj feem xyuam nrog database, nws yuav yog SQL Exception. Cov no yog qee qhov piv txwv ntawm kev kuaj xyuas. Nyob rau hauv tag nrho cov no, daim ntawv thov txuas nrog ib qho kev pab sab nraud. Hauv kev txheeb xyuas qhov kev zam, nws yog qhov yuav tsum tau ua qhov kev zam. Yog tias nws tsis ua haujlwm, qhov tseeb txaus ntawm qhov kev pab cuam yuav raug txiav, thiab cov ntaub ntawv hauv chav kawm yuav tsis raug tsim tawm. Qhov yuam kev tuaj yeem daws tau siv qhov sim, ntes thaiv.
Daim duab 01: Tshawb xyuas Kev Tiv Thaiv Kev Tiv Thaiv
Raws li cov saum toj no, FileReader nyeem cov ntaub ntawv los ntawm cov ntaub ntawv. Cov ntaub ntawv text1.txt tsis muaj nyob hauv qhov chaw teev tseg. Cov cai uas tuaj yeem muab qhov kev zam yog muab tso rau hauv qhov sim thaiv. Cov lus los luam tawm yog nyob rau hauv lub catch block. Raws li tsis muaj cov ntaub ntawv hu ua text1.txt, qhov no ua rau FileNotFoundException. Los ntawm kev siv tshwj xeeb tuav, cov lus tau luam tawm rau lub vijtsam.
Dab tsi yog Unchecked Exceptions hauv Java?
Cov kev zam tsis tau txheeb xyuas tsis raug txheeb xyuas los ntawm tus sau. Tsis zoo li hauv kev txheeb xyuas, nrog rau qhov tsis raug txheeb xyuas, daim ntawv thov Java tsis txuas nrog cov peev txheej sab nraud xws li cov ntaub ntawv, database lossis ib lub cuab yeej. Qee qhov kev zam tsis raug txheeb xyuas yog Arithmetic, ArrayOutOfBound thiab NullPointer Exceptions.
int a=10, b=0;
int div=a/b;
System.out.println(div);
Qhov no yuav ua rau muaj kev zam lej lej vim tias dhia 'a' los ntawm xoom. Xa mus rau tus lej hauv qab no.
Daim duab 02: Tus lej lej tshwj xeeb tuav
Raws li cov kev pab cuam saum toj no, qhov sib txawv a yog tus nqi suav. Qhov sib txawv b yog 0. Qhov kev faib ntawm ob tus lej no yog faib los ntawm xoom. Yog li ntawd, nws yuav ua rau muaj kev zam lej lej. Nws tuaj yeem siv tau los ntawm kev sim-catch block. Cov nqe lus uas tuaj yeem ua rau qhov kev zam yog muab tso rau hauv qhov sim thaiv. Cov lus yuav tsum tau muab tso rau hauv lub catch block.
Saib daim ntawv hauv qab no ntawm tus lej.
int array1={1, 2, 3, 4, 5};
System.out.println(array1[5]);
Qhov no yuav ua rau muaj kev zam. Lub array1 yog ib qho array nrog 5 yam. Qhov pib Performance index ntawm array yog xoom. Kev luam tawm 5th qhov ntsuas tus nqi ua rau muaj kev zam vim nws tsis muaj kev cia siab. Qhov siab tshaj plaws Performance index ntawm array1 yog 4.
Daim duab 03: ArrayOutOfBound Exception Handling
Raws li cov kev pab cuam saum toj no, array1 muaj 5 yam. Kev luam tawm lub caij nrog qhov ntsuas 6 yuav ua rau muaj kev zam vim nws tsis muaj kev cia siab. Qhov siab tshaj plaws qhov ntsuas uas tuaj yeem khaws cia hauv array1 yog 5. Cov lus yuam kev luam tawm los ntawm kev ua tiav qhov ntes thaiv.
Dab tsi yog qhov zoo sib xws ntawm Kev Tshawb Fawb thiab Tsis Txheeb Xyuas Hauv Java
Ob qho tib si kuaj thiab tsis txheeb xyuas hauv Java yog hom kev zam hauv Java
Dab tsi yog qhov txawv ntawm Kev Tshawb Fawb thiab Tsis Tshawb Fawb Hauv Java?
Checked vs Unchecked Exception in Java |
|
Kev kuaj xyuas yog qhov yuam kev runtime uas tau kuaj xyuas los ntawm tus sau. | Ib qho kev zam tsis tau txheeb xyuas yog qhov yuam kev khiav lub sijhawm uas tsis tau kuaj xyuas los ntawm tus sau. |
tshwm sim | |
Thaum muaj qhov kuaj pom tshwm sim, daim ntawv thov Java txuas nrog rau cov peev txheej sab nraud xws li cov ntaub ntawv, ntaus ntawv lossis database. | Thaum muaj qhov tsis raug txheeb xyuas tshwm sim, daim ntawv thov Java tsis txuas nrog cov peev txheej sab nraud. |
Examples | |
IOException, FileNotFoundException, SQLException yog qee qhov piv txwv ntawm kev kuaj xyuas. | Qhov kev zam lej lej, ArrayOutOfBoundException, NullPointerException yog qee qhov piv txwv ntawm qhov tsis tau txheeb xyuas. |
Summary – Kos vs Unchecked Exception in Java
Ib qho kev zam yog ib qho kev tshwm sim uas cuam tshuam qhov kev ua tiav ntawm qhov kev pab cuam ntws. Muaj ob hom kev zam. Lawv hu ua checked exceptions thiab unchecked exceptions. Kab lus no tau tham txog qhov sib txawv ntawm qhov kev txheeb xyuas qhov kev zam thiab tsis tau kuaj xyuas. Qhov sib txawv ntawm kev txheeb xyuas thiab tsis tau txheeb xyuas qhov tshwj xeeb hauv Java yog qhov kev kuaj xyuas tshwj xeeb yog kuaj xyuas los ntawm cov neeg sau ntawv thaum qhov tsis tau txheeb xyuas tsis raug txheeb xyuas los ntawm lub compiler. Raws li kev zam cuam tshuam rau qhov tseeb khiav ntawm qhov kev ua tiav, nws yog qhov kev xyaum ua haujlwm zoo los daws lawv.