Checked Exception vs Runtime Exception
Kev zam yog yam tshwj xeeb ntawm cov xwm txheej, uas tuaj yeem cuam tshuam qhov kev ua haujlwm ib txwm muaj. Lub npe tshwj xeeb yog los ntawm "exceptional event". Pov ib qho kev zam yog cov txheej txheem ntawm kev tsim ib qho khoom tshwj xeeb thiab xa tawm mus rau lub sijhawm ua haujlwm. Thaum lub sijhawm khiav haujlwm tau txais cov khoom tshwj xeeb, nws yuav sim nrhiav ib tus neeg los tswj nws hauv pawg hu xov tooj los ntawm kev hla nws hauv qhov kev txiav txim rov qab (hauv cov txheej txheem hu ua). Lub sijhawm ua haujlwm tau ua tiav yog tias nws pom ib txoj hauv kev nrog tus neeg ua haujlwm tshwj xeeb. Exception handler yog ib qho thaiv ntawm cov cai uas tuaj yeem ua raws li qhov kev zam. Yog tias lub sijhawm khiav haujlwm pom tus neeg tuav haujlwm tsim nyog, nws yuav dhau qhov khoom tshwj xeeb rau tus tuav. Qhov no yog hu ua ntes qhov kev zam. Txawm li cas los xij, yog tias qhov kev zam tsis tuaj yeem daws tau, qhov kev zov me nyuam yuav xaus. Hauv Java, kev zam tau txais los ntawm Throwable class. Checked Exceptions yog cov kev zam uas tuav tswj los ntawm lub compiler. Runtime exceptions yog ib hom kev zam, uas tsis tau kuaj los ntawm lub compiler.
Dab tsi yog Kev Tshawb Fawb?
Checked Exceptions yog cov khoom ntawm chav kawm java.lang.exception lossis nws cov subclasses (tshwj tsis yog cov java.lang. RuntimeException thiab nws cov subclasses). Txheeb xyuas cov kev zam yog "tsim" thaum lub sijhawm sau. Qhov ntawd txhais tau hais tias tus programmer yuav tsum tau ntes lossis pov cov kev zam no, lossis lwm yam kev sau ua ke yuav yws (ua rau muaj qhov yuam kev compiler). Vim li no, ntau qhov kev kuaj xyuas yog paub zoo rau cov programmers. Piv txwv li, IOException thiab nws cov chav kawm sub raug kuaj xyuas, thiab thaum twg tus programmer tab tom cuam tshuam nrog kev nkag lossis hloov kho cov ntaub ntawv, compiler xyuas kom paub tseeb tias txhua qhov ua tau IOExceptions raug saib xyuas los ntawm tus programmer.
Dab tsi yog Runtime Exception?
Runtime Exceptions suav nrog java.lang. RuntimeException thiab tag nrho nws cov chav kawm sub. Kev hais lus theoretically, lawv ua haujlwm tib lub hom phiaj raws li kev kuaj xyuas, thiab tuaj yeem raug pov lossis ua haujlwm zoo li kuaj xyuas, tab sis kev tuav ntawm lawv tsis raug tswj los ntawm cov neeg sau ntawv. Yog li ntawd, Runtime exceptions belongs rau tsev neeg ntawm unchecked kev zam. NullPointerException, NumberFormatEception, ClassCastException thiab ArrayIndexOutOfBoundsException yog ib qho kev zam runtime hauv Java.
Dab tsi yog qhov txawv ntawm Kev Tshawb Fawb Kev Tshawb Fawb thiab Kev Ua Si Sijhawm?
Txawm hais tias, ob qho tib si kuaj xyuas thiab kev zam lub sijhawm ua haujlwm tsis xav tau tshwm sim thaum lub sijhawm ua tiav ntawm qhov program, lawv muaj qhov sib txawv. Txheeb xyuas qhov tshwj xeeb tuav yog tswj hwm los ntawm lub compiler, tab sis kev zam lub sijhawm khiav tsis yog. Yog li ntawd, kev txheeb xyuas cov kev zam yuav tsum tau muab pov tseg lossis ua haujlwm txhawm rau txhawm rau txhawm rau sau cov lej, tab sis tsis muaj qhov yuav tsum tau hais txog kev zam lub sijhawm ua haujlwm. Yog li ntawd, cov kev zam rau lub sijhawm ua haujlwm tau raug txheeb xyuas tsis tau txheeb xyuas qeb nrog rau qhov yuam kev.
Qhov tsis zoo nrog kev kuaj xyuas yog qhov programmer yuav tsum tau ua txawm tias nws tsis paub yuav ua li cas. Yog li, yog tias tus programmer cia li cuam tshuam qhov kev zam tshiab yam tsis tau qhwv tus thawj, pawg kab uas yog tus thawj qhov kev zam yuav ploj mus. Qhov no yog qhov kev zam runtime tuaj yeem ua ke. Vim hais tias txhua qhov kev zam runtime tuaj yeem ua haujlwm hauv ib qho chaw, yog li programmers tuaj yeem sau cov lej tsawg dua. Ntawm qhov tod tes, txij li kev txheeb xyuas qhov kev zam yuav tsum raug ntes, tsis muaj qhov xav tsis thoob rau tus programmer. Nws yuav nco ntsoov paub qhov kev kuaj xyuas tshwj xeeb tuaj yeem cuam tshuam los ntawm ib txoj hauv kev. Contrary to this, ntau yam kev zam runtime tuaj yeem cuam tshuam yam tsis muaj kev paub txog tus programmer.