İ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.
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(); } |
1 | CloneNotSupportedException | 41 | AWTException |
2 | ClosedChannelException | 42 | AclNotFoundException |
3 | DataFormatException | 43 | ActivationException |
4 | DatatypeConfigurationException | 44 | AlreadyBoundException |
5 | DestroyFailedException | 45 | ApplicationException |
6 | EOFException | 46 | ArithmeticException |
7 | Exception | 47 | ArrayIndexOutOfBoundsException |
8 | ExecutionException | 48 | AssertionException |
9 | ExpandVetoException | 49 | BackingStoreException |
10 | FileLockInterruptionException | 50 | BadAttributeValueExpException |
11 | FileNotFoundException | 51 | BadBinaryOpValueExpException |
12 | FishFaceException | 52 | BadLocationException |
13 | FontFormatException | 53 | BadStringOperationException |
14 | GSSException | 54 | BatchUpdateException |
15 | GeneralSecurityException | 55 | BrokenBarrierException |
16 | IIOException | 56 | CertificateException |
17 | IOException | 57 | ChangedCharSetException |
18 | IllegalAccessException | 58 | CharConversionException |
19 | IllegalArgumentException | 59 | CharacterCodingException |
20 | IllegalClassFormatException | 60 | ClassNotFoundException |
21 | MimeTypeParseException | 61 | IllegalStateException |
22 | NamingException | 62 | IndexOutOfBoundsException |
23 | NegativeArraySizeException | 63 | InputMismatchException |
24 | NoSuchElementException | 64 | InstantiationException |
25 | NoSuchFieldException | 65 | InterruptedException |
26 | NoSuchMethodException | 66 | InterruptedIOException |
27 | NoninvertibleTransformException | 67 | IntrospectionException |
28 | NotBoundException | 68 | InvalidApplicationException |
29 | NotOwnerException | 69 | InvalidMidiDataException |
30 | NullPointerException | 70 | InvalidPreferencesFormatException |
31 | NumberFormatException | 71 | InvalidTargetObjectTypeException |
32 | ObjectStreamException | 72 | InvocationTargetException |
33 | ParseException | 73 | JAXBException |
34 | ParserConfigurationException | 74 | JMException |
35 | PrintException | 75 | KeySelectorException |
36 | PrinterException | 76 | LastOwnerException |
37 | PrivilegedActionException | 77 | LineUnavailableException |
38 | PropertyVetoException | 78 | MalformedURLException |
39 | ProtocolException | 79 | MarshalException |
40 | RefreshFailedException | 80 | MidiUnavailableException |
Hiç yorum yok:
Yorum Gönder