20 Nisan 2012 Cuma

JAVADA EXCEPTIONLAR

Calisma zamani gozlenilmeden 1 xeta yaranmasina exception deyilir.1 exception-in yaradilib programin icerisine atilmasi emeliyyatina throwing deyilir.Yaranan 1 exceptionu tutub problemin hell olmasi ise catching adlanir.Exceptionu tutmaga calisan kod blogu catch blok adlanir.1 Exception yarananda yaddasin heap hissesinde new() keywordu ile yaradilir.Yaranan bu exception obyektinin icerisine xetanin yarandigi setir yerlesdirilir.Exceptionda olan finally metodu xeta olmasina baxmayaraq calisacaq hissedir.Eger try bloqunda return ifadesi varsa return evvel execute olunur ve sonra ise finally execute olunur.Eger exit() cagirilsa o zaman finally bloguna girilmez.
1 class-da try{} blogu catch ve finally olmadan yazilsa xeta verecek.Eger try ve catch bloglari arasina her hansi 1 kod setiri yazilsa xeta verecek.meselen:
try{
}
System.out.print("hello");
catch(Exception e){
}

Exceptionlar java.langg.Exceptionun subclasslaridirlar.Throwable Exceptionun super classidir,Object ise hamisinin super class-dir.
Throwable 2 yere bolunur:

  • Error
  • Exception
Error xususile sistem problemlerile bagli olur ve onlarin qarsisini almaq cox cetindir ve hardasa imkansizdir.
Exceptionlar 2 yere bolunr:

  • Checked exception
  • Unchecked exception veya Runtime Exception.
Checked Exceptionlar:

  • ClassBotFoundException
  • IOException buda 2 yere bolunur: 1) EOFException ve 2)FileNotFoundException
  • ParseException
Unchecked veya Runtime Exceptionlar ise:

  • ArithmeticException
  • ClassCastException
  • IllegalArgumentException buda 2 yere bolunur:1) IllegalThreadStateException ve 2) NumberFormatException
  • IndexOutOfBoundsException buda 2 yere bolunur:1)StringIndexBoundsException ve 2) ArrayIndexOutOfBoundsException
  • NegativeArraySizeException
  • NullPointerException
  • SecurityException
Eger 1 Exceptionun sub classlari varsa o zaman try catch bloqlarin yazanda 1ci catch bloqunda o exceptionun sub-i sonra ise super-i gelmelidir.Eger eksine olsa Xeta verecek.Meselen:
try{ }
catch(IOException e){}
catch(FileNotFoundException ex) { }        // burada xeta verecek.Cunki 1ci subi sonra superi gelmelidi.balacadan boyuye.

Eger 1 metodun basinda throws yazilibsa bu o demekdirki bu metodu kim istifade etse try ve catch atmalidir.
void doMore() throws IOException { ... }
bu kodu istifade edende mutleq try ve catch atilmalidir.
RuntimeException ,Error ve onlarin butun sublari unchecked exceptiondurlar. ve onlari mueyyen etmek veya handle etmek olmur.Yerde qalan diger exceptionlar ise checked olurlar ve onlar java.lang.RuntimeException-dan toremirler.Checked Exceptionlar kodda istenilen yerde tutula bilerler.Eger 1 checked exception atan metod cagirilirsa ve tutulmursa o zaman kod compile olmayacaq.

Exceptionlar JVM (yeni runtime) ve Programmatic ( yeni compiler) exception olmaqla 2 yere bolunurler.
JVM exceptionlar JVM terefinden atilir.
Programmatic Exceptionlar ise API programmerler veya applicationlar terefinden atilir.
Programmatical exceptionlar asagidakilardir:

  • AssertionError
  • NumberFormatExcepption
  • IllegalArgumentException
  • IllegalStateException
Yerde qalanlarin hamisi JVM exceptionlardir.

Hiç yorum yok:

Yorum Gönder