1 Mayıs 2013 Çarşamba

Java hata Yakalama (Try-Catch)




İSTİSNA YÖNETİMİ (EXCEPTION)
•İstisnalar, programımızın çalışma zamanında yani program çalışırken kullanıcı kaynaklı yanış veri tipi girme gibi daha birçok şekilde ortaya çıkan olağan dışı durumlardır.
•Programcılık dilinde "programın kırılması" ile adlandırılan olaylarda aslında bir istisnanın meydana geldiğini göstermektedir. Örneğin bir sayının sıfıra bölünmesi gibi
Exception in thread «main» java.lang.ArithmeticException: / by zero at Donusum1.main(Donusum1.java:14)
•Başka bir istisna örneği ise uyumsuz tipler arasında değer aktarımı gerçekleştirmeye çalışmaktır. Bir programcı olarak kod üzerinde her ne kadar detaylı çalışma yapsakta kullanıcı faktörü göz önüne alındığında çalışma zamanı hatalarının ( Runtime Error ) ortaya çıkması olasıdır. 
 Çalışma zamanında ortaya çıkabilecek bazı istisnalar.
int sayi1 = 15;
int sayi2 = 0;
int sonuc = 0;
sonuc = sayi1 / sayi2;       // Sıfıra bölünme hatası        
sonuc = Integer.parseInt("Selam"); // Tür dönüşüm hatası
Exception in thread "main" java.lang.ArithmeticException: / by zero at Donusum1.main(Donusum1.java:14)
Exception in thread "main" java.lang.NumberFormatException: For input string: "Selam" at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at Donusum1.main(Donusum1.java:15)
•Hataları ve istisnaları belirlemek ve bu istisnaları yakalayıp yönetmek için bir takım işlemler yapmak gerekmektedir.
•Bir hatayı ortaya çıkmadan önce belirleyebilmek için öncelikle  çalışma anı hatalarının ortaya çıkabileceği kodları belirlemek gerekir. Bu işlem yapıldıktan sonra önceden tanımlanmış bazı ifadelerden birini kullanarak istisna yakalama işlemi gerçekleştirilir. Bunlar;
•1. try catch
•2. try finally
•3. try catch finally

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int sayi1 = 15;
int sayi2 = 0;
int sonuc = 0;
try
{
    sonuc = sayi1 / sayi2;       // Sıfıra bölünme hatası          
}
catch(Exception hata)
{
    JOptionPane.showMessageDialog(null, "Hata var: " + hata );
}
try
{
     Sonuc = Integer.parseInt("Selam"); // Tür dönüşüm hatası
}
catch(Exception hata)
{
     JOptionPane.showMessageDialog(null, "Hata var: " + hata );
}
--------------------------------------
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
try
{
   String[] Aylar = {"Ocak","Şubat","Mart","Nisan","Mayıs"};  
   for(int i = 0; i < 10; i++)
    {
        System.out.print(Aylar[i] + " ");
    }
}
catch(ArrayIndexOutOfBoundsException  hata)
{
  System.out.println("Hata var : " + hata.toString());
}
finally
{
  System.out.println("Bitti");
}
------------------------------------------------
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
try {
      InputStreamReader SatirOku = new InputStreamReader(System.in);
      BufferedReader br = new BufferedReader(SatirOku);
      while (true)
      {
        System.out.print("Yarıçapı Girin: ");
        String str = br.readLine();
        double yaricap;
        try
        {
        yaricap = Double.valueOf(str).doubleValue();
        }
        catch (NumberFormatException nfe)
        {
          System.out.println("Hatalı veri tipi yeniden girin");
          continue;
        }
        if (yaricap <= 0)
        {
          System.out.println("Yarıçap pozitif tamsayı olmalı!");
          continue;
        }
        double alan = Math.PI * yaricap * yaricap;
        System.out.println("Alan: " + alan);
        return;
      }
    }
  catch (Exception e)
  {
    e.printStackTrace();
  }
-----
1CloneNotSupportedException41AWTException
2ClosedChannelException42AclNotFoundException
3DataFormatException43ActivationException
4DatatypeConfigurationException44AlreadyBoundException
5DestroyFailedException45ApplicationException
6EOFException46ArithmeticException
7Exception47ArrayIndexOutOfBoundsException
8ExecutionException48AssertionException
9ExpandVetoException49BackingStoreException
10FileLockInterruptionException50BadAttributeValueExpException
11FileNotFoundException51BadBinaryOpValueExpException
12FishFaceException52BadLocationException
13FontFormatException53BadStringOperationException
14GSSException54BatchUpdateException
15GeneralSecurityException55BrokenBarrierException
16IIOException56CertificateException
17IOException57ChangedCharSetException
18IllegalAccessException58CharConversionException
19IllegalArgumentException59CharacterCodingException
20IllegalClassFormatException60ClassNotFoundException
21MimeTypeParseException61IllegalStateException
22NamingException62IndexOutOfBoundsException
23NegativeArraySizeException63InputMismatchException
24NoSuchElementException64InstantiationException
25NoSuchFieldException65InterruptedException
26NoSuchMethodException66InterruptedIOException
27NoninvertibleTransformException67IntrospectionException
28NotBoundException68InvalidApplicationException
29NotOwnerException69InvalidMidiDataException
30NullPointerException70InvalidPreferencesFormatException
31NumberFormatException71InvalidTargetObjectTypeException
32ObjectStreamException72InvocationTargetException
33ParseException73JAXBException
34ParserConfigurationException74JMException
35PrintException75KeySelectorException
36PrinterException76LastOwnerException
37PrivilegedActionException77LineUnavailableException
38PropertyVetoException78MalformedURLException
39ProtocolException79MarshalException
40RefreshFailedException80MidiUnavailableException


Hiç yorum yok:

Yorum Gönder