Rorohiko, Hōtaka
Java: okotahi me ratou whāwhā
ka whakahaere i tetahi hōtaka stably anake, ki te debugged te waehere pūtake kei, a ki reira he kore tikanga e taea meinga āhuatanga i möhio. kawea te tukanga o te hopu rahunga taea te roto i runga i te atamira hōtaka. Ki te mahi i tenei, e te kaiwhakawhanake ki pūkete nga putanga e tūmanakohia katoa me whakamātau ki te whakawhāiti i te pānga o te hapa kia e kore i taea e pokanoa te reira ki te mahi o te hōtaka, arahi ki tona tiango ranei.
A, no te ai hiahia koe okotahi whāwhā
I roto i Java, e taea te meinga te okotahi i te tāuru kaiwhakamahi hē, kahore he take mo te rauemi ohorere hōtaka momotunga whatunga ranei. Hoki te whakamahi whakamarie i hanga e te kaiwhakawhanake tono, me te whakahaere koe i te takanga o ngā āhuatanga ohorere. e kore e whai ki te tatari ki te whakamutu i te tono i te kiritaki, ki te ngaro raraunga rite te hua o te okotahi tē mahi, noa wahi o te karere puta kī i haere he tahi mea ranei.
Handling okotahi Java
He aha ki te whakaaro? Ko te reo Java kua ona ake taumahinga okotahi whāwhā hanga-i roto i. O te akoranga, mau he ōrau nui o hapa e tetahi atamira kohinga, te pūnaha ka whakamōhio aunoa i whakamahi he reira kore taea. Ko reira ko tenei ahua o te okotahi e puta i te wā. kia te kaiwhakawhanake taea ki te pahika reira me ki hoahoa te waehere kia e kore i meinga ai he hapa, a whawha ana e ia i roto i te ara motuhake, me te hoatu i runga i te mana ki tetahi atu peka.
I roto i Java, whakaritea tenei e te taupatupatu ki te hopu i te okotahi, na kua mohiotia nga raruraru angamaheni, me te whai i te tauira mahi paerewa.
te whakarerenga angamaheni
Te tauira māmā, i roto i nei e taea te whiwhi koe i te okotahi - tenei wehenga. Ahakoa tona tapatahi, i roto i ngā, me te mea he kaiwehewehe, kia kore, e ka hua i roto i te hapa. Well, ki te he e taea ki te matapae i te putanga o te mua, me te ārai i. Ko e kore ko tenei kōwhiringa tonu e wātea ana, na hopu te okotahi kia whakaritea tika i roto i te hui o "wehenga i te kore".
rite tenei te tikanga Java ki te haukoti hapa tukatuka titiro:
- Kei te hanga ahanoa okotahi puranga, me te ki tetahi atu;
- Ko haukotia te akoranga tūturu o te hōtaka;
- Kei te ngana whakarerenga tikanga ki te kitea he ara kē ki te haere tonu i te waehere;
- Kimihia he mahi haumaru o te hōtaka i roto i te pūtukatuka, mahi ranei whakahokia, ranei ka puta iteraa o te okotahi i roto i te ara motuhake.
Ko te tauira māmā o te hanga i tētahi hapa kia titiro rite tenei:
ki te (te whakakahore ==)
maka NullPointerException hou ();
Here, takina te he tāupe te i arawhiti, arā, e kore te mea ahakoa te tohutoro ki te ahanoa korenga. Ki te mea kua puta ake tenei āhuatanga me te hiahia whāwhā motuhake, maka te okotahi e te NullPointerException hou taunga ().
Torutoru kōrero e pā ana ki kupumatua
A, no te e pā ana ki okotahi he maha e tika ana ki te whakamahi i te Java kupu matua ki te kōrero ki te mahi ngā. Tenei reo o ratou hōtaka e rima:
- Whakamātauria. Kei te ana tēnei kupu matua tutaki kua, a te auraa te whakawhitinga ki te wāhanga o te waehere e taea maka he okotahi. Poraka iti Whītiki {}.
- Hao. haukotinga te reira i te momo okotahi hiahiatia, ka hapai i te reira tika.
- Hei whakamutunga. Ko tenei kupu matua kōwhiringa me te whakamahia ki te mahi i tetahi wahi etahi o waehere, e hiahiatia ana e te tonu, ara ki te kore e mau kahore okotahi. Tāpiri tika i muri poraka piro.
- Maka - ka taea te waihanga e koe Java okotahi i hea.
- Whiua - he kupu matua whakanohoia e te roto i te waitohu tikanga. Te auraa ra, e taea te waehere e whai ake nei maka he okotahi o tenei momo Java. tavini ana tēnei tohu rite te tohu ki ngā e hiahia ki te mea hei maunga i roto i te ngakau - e kore e taea e te tikanga mahi rite tūmanakohia o ia.
Te hopu me te piro
Tuku ki okotahi Java, fakanatula riro e ka whawha reira i roto i te ara motuhake. Ko te ara tino watea ki te mahi i te reira, ki te taiepa te wāhanga waehere e atu i roto i te poraka. E kia roto i te okotahi. A, no te rere koe i tenei waehere, ka kitea te mīhini mariko he āhuatanga ohorere, mahino e reira ko te poraka tino me te whakahaere whakawhitinga ki te teihana ki te tukatuka.
Kei te takai i te waehere Java i roto i te piro poraka motuhake, i roto i nei kia te okotahi. Ko te kupu, whakanohoia ia e rave rahi mau tupuraa mana'o-e e ka mau i roto i te wahi kotahi, e kore raspolzayas waehere.
Ko te waehere tino angamaheni ki te wae tukatuka ko e whai ake:
tamata {
// Anei ko etahi waehere e kia whakaputa he okotahi
} Catch (Tip_isklyucheniya_1 identifikator_1) {
// Here reira he okotahi whāwhā, kia rite ki tona momo me tikanga;
} Catch (Tip_isklyucheniya_2 identifikator_2) {
// Here reira he okotahi whāwhā, kia rite ki tona momo me tikanga;
}
pūrongo hopu Kupu matua taua waehere, a te matawai i te reira mo te okotahi, ki kia tukinotia rite whakaahuatia i raro nei, ngā e ū ai ki tona momo. Ka taea te whakamahi i te pūtāutu i roto i te tukanga i te poraka waehere rite tohenga.
I te pae hopea
Ka rite ki ka reira mārama i te pene o mua, hopu poraka hopu okotahi, me te tukatuka ratou. Ko te tino maha whakatika te āhuatanga i reira kia rere etahi waehere, ahakoa o i mau ranei ratou hape. Hoki tenei i reira ko te kupu matua te mutunga. Kei te whakamahia te reira ki te whakapiki ake i te uara o te ngā porotiti, kati kōnae hononga whatunga ranei.
E whakaatu ana tēnei wahi e rave rahi poraka hao whakaaro tikanga hopu okotahi. Hei tauira, te waehere roto i roto i te piro mahia he āhuatanga ohorere pērā i Cold. Na, ka whakaaturia te papatohu faaiteraa «Mau matao!» Na «Ko e ki te tahi mea ngakau e pā ana ki?». Ko, mahi ana te pae hopea poraka e i roto i tetahi take.
I roto i te meka, ki te ara karo triggering hopea i reira. Kei te hono te reira ki te otinga o te mīhini mariko. Kimihia pehea ki te whakatinana i te reira, ko reira taea i runga i te wāhi tuwhera Ipurangi.
kupu matua maka
Maka mahia he okotahi. Ko tōna wetereo ko e whai ake:
maka NewException hou ();
He Kei te hanga i te okotahi hou ki te momo NewException (). Ka rite ki te whakaurua kē he momo taea te whakamahi i roto i te akomanga whare pukapuka Java paerewa, me te tautuhia i mua i te kaiwhakawhanake o ona ake production.
Tenei hoahoa Ko wāhanga o te whakaahuatanga o tetahi tikanga, e kia ka te karanga puta i roto i te poraka piro, i roto i te tikanga ki te e taea ki te haukoti i te reira.
ka whiua Kupu matua
He aha te ki te i roto i te tukanga o te whanaketanga o te āhuatanga i reira ka taea e te tikanga maka he okotahi, engari e kore e taea e ki te hapai i tika. Ki tenei mutunga, i roto i te hainatanga tikanga āta whiua te kupu me te momo o okotahi taea.
Tenei tapanga Ko te ahua o te tohu mō ngā kiritaki e kore e taea e ki te hapai i to ratou taua okotahi te tikanga. I tua atu, ki te tirohia te momo hapa te, na ka faahepo i te taupatupatu āta ui hoki reira.
Whakamātauria ki ngā rauemi
Kua whakaurua e te Java putanga 7 ngā te auaha matua, ka rite ki te poraka maimoatanga piro ki ngā rauemi.
E hanga tokomaha taonga i roto i Java, i muri kia whakamahi kia tutakina iho ki te tiaki i ngā rauemi. I mua i te reira ki te tango i tenei ki pūkete, me te mutu ā enei wā. Na, Heoi, puta ratou atanga AutoClosable. E tauturu te reira ki te kati aunoa ahanoa whakamahia kē whakanohoia i roto i te poraka piro. Na roto i tenei huarahi, kua riro te reira atu watea ki te tuhituhi waehere i roto i te tino nui haere tona pānui.
akomanga Own okotahi Java
Ko te kaihanga o te reo papatono whakaahuatia ka mau ki te pūkete maha ngā āhuatanga o te momo hoahoa o ohorere. Heoi, ki katoa o te kōwhiringa ārai te putanga o ngā kore e mahi, na i roto i whakatinana Java te kaha ki te tautuhi ratou ake okotahi, he reira e tika ana ki te hiahia o te waehere ngā.
Ko te ara māmā ki te waihanga - ki riro i te tika tino ki te horopaki ahanoa.
He tupu ki te riro i Okotahi, te piha haapiiraa whakamahia e te ki te tautuhi i tō ake okotahi. I roto i te MyException E rua constructors - he taunoa, te tuarua - ki te tautohe momo MAORI Aho.
Na, i roto i te f tikanga FullConstructors akomanga tūmatanui whakatinana, e kei te waitohu whiua MyException. tikanga o tēnei kupu matua e taea f maka rite Java MyException okotahi. I tua atu, i roto i te tinana tikanga hua ngā mōhiohio kuputuhi putanga ki te papatohu ake me te whakatupuranga MyException tūturu, i te taunga.
Ko paku rerekē i te tetahi tuatahi e ina hanga e koe he okotahi, kua haere te reira i te tawhā string, ka e kia kitea i roto i te papatohu i hopu i te tikanga tuarua. I roto i te matua, ko reira mārama e f () me g () e whakanohoia i roto i te arowhai piro me whirihora hao kupu matua ki te hopu MyException. Ko te hua o te tukatuka ko ki te whakaatu i tētahi karere hapa ki te papatohu:
Ko te kupu ka haere mai te reira ki te tāpiri i Java okotahi, na tona ringa ake.
okotahi architecture
Ka rite ki ki ahanoa katoa i roto i Java, kua riro hoki okotahi, me te whai i te hanganga aroākapa. Ko te huānga pakiaka o hapa katoa maka i roto i tenei reo hōtaka ko te piha java.lang.Throwable. Riro i ia momo e rua - Hapa me te Okotahi.
Hapa - tohu te hapa tino me ko okotahi Java kore taki. Mōhio me te tukatuka raraunga pērā i roto i te nuinga o te wā puta i te wā hoahoa me kahore i roto i te hiahia o te whakauru i roto i te waehere tono whakamutunga.
Ko te piha haapiiraa tino nuitia whakamahia mō te hanga me te tātari i te okotahi he Okotahi. E, i roto i te tahuri, kua wehea ki te maha manga, tae atu RuntimeException. E RuntimeException He okotahi wāhaere, e ko, tupu i roto i te hōtaka. He unverifiable akomanga katoa i takea mai i reira.
E mea pinepine i reira he okotahi
I roto i te okotahi Java, aroaro te rārangi o nei te raro nei, kei te whakamahia te nuinga maha, kia kia whakaahua e koe ia o ratou i roto i ngā kōrero:
- ArithmeticException. Ēnei ngā hapa e pā ana ki ngā mahi tauhanga. Ko te tauira tino glaring - te wehenga i te kore.
- ArrayIndexOutOfBoundsException - he tono ki te tau huānga ngohi e nui te tapeke o tona roa.
- ArrayStoreException - ngana ki te tautapa i te momo huānga hotokore o te ngohi.
- ClassCastException - he whakamātau ki te kawe i te kotahi momo he ki tetahi.
- IllegalArgumentException - te whakamahi i te tautohe he i roto i te piiraa tikanga.
- NegativeArraySizeException - he okotahi, ina hanga e koe he ngohi rahi kino o.
- NullPointerException - hē o tohutoro ki te korenga.
- NumberFormatException - puta ka ki te faafariuraa string he tau tōpū.
- UnsupportedOperationException - e kore te mahi e tautokona ana.
Enei tauira tohu nga momo o okotahi Java kore taki. Otiia kia titiro takina:
- ClassNotFoundException - Class kihai i kitea.
- IllegalAcccessException - rāhui i te uru ki te piha haapiiraa.
- InterruptedException - arumanga o te rere.
- NoSuchFieldException - i reira he kore āpure e hiahiatia ana.
Ko te tikanga o te okotahi
Korero o okotahi pinepine tūpono kia tuhia e ratou tikanga i roto i te hoahoa, ka taea te kite a hē. Next ko te rārangi poto e whakamārama i roto i atu taipitopito, te wahi i reira kia kia he āhuatanga ohorere.
NullPointerException. Ko te take tino tuatahi ka reira ko te okotahi, he ki te karanga ki te tohutoro ki te ahanoa e he rite ki te korenga. Reira atu hoki ki nga tikanga tauira piha kore. kia NullPointerException kia maka i roto i te take o te whiwhi i te roa ngohi korenga rite. Ki te karo i taua āhuatanga, ka ia wā tirohia pae i runga i te korenga.
ArrayIndexOutOfBoundsException. e kore e taea e tetahi hōtaka tīariari i waho te whakamahi o ngā huānga. Nä, ko te kaikä auau ki reira ka taea e whakaputa me te hapa. puta Okotahi ka tamata te kaiwhakawhanake ki te uru i te huānga ngohi e kore ko e i roto i te rārangi taupū. Hei tauira, kei runga ake ranei i raro kore roa te uara tonoa. Tino maha ko te hua o te meka e i roto i te ngohi haamata ki te kaute o te kore.
ngā kitenga
Handling Java okotahi - he taiao taputapu kaha, e nui whakahaere te mahi o te kaihātepe me taea ki a ia te hanga i te pokekore, me te kowaiwai o hapa waehere. I te tono mahi pehea maeneene me te pūmau tei runga i te mana me te ingoa o te kaiwhakawhanake.
O te akoranga, i roto i te atu iti iho ranei hōtaka ohie ki te ahu āhuatanga rerekē nui māmā. Otiia i roto i complexes nui aunoa mo te torutoru rau mano rārangi ko reira taea anake rite ki te hua o te whakamātautau wā roa-a patuiro.
Hoki Java hapa okotahi i nei ara ake i roto i te tahi mau tono, etahi kamupene whakahere he utu ina kitea e ratou inapō. Rawa fakahounga'i e te hunga e meinga he takahi o te pūmanawa kaupapa here haumarutanga.
Similar articles
Trending Now