ile Nesne Yönelimli Programlama
14 İstisnalar(Exceptions)
İçerik
▪ İstisnalar(Exceptions)
▪ Try-Catch-Finally Kullanımı
▪ Throw ile Hata Fırlatılması
İstisnalar
public class Islem {
public static void main(String[] args) { int a,b,c;
a=5;
b=0;
c=a/b;
} }
İstisnalar(Exceptions)
public class Islem {
public static void main(String[] args) { int a,b,c;
try { a=5;
b=0;
c=a/b;
}catch (Exception e) {
System.out.println("Bir hata oluştu =>"+e.toString());
}
Finally (Hata olsa da olmasa da çalışır)
public class Islem {
public static void main(String[] args) { int a,b,c;
try { a=5;
b=0;
c=a/b;
}catch (Exception e) {
System.out.println("Bir hata oluştu =>"+e.toString());
}finally {
System.out.println("Program sonlandı");
}
Dizi Sınırı Aşılması (ArrayIndexOutOfBound)
public class Istisnalar {
public static void main(String[] args) { int[] a= {1,3,5,7,9};
System.out.println(a[5]);
} }
Dizi Sınırı Aşılması (ArrayIndexOutOfBound)
public class Istisnalar {
public static void main(String[] args) { try {
int[] a= {1,3,5,7,9};
System.out.println(a[5]);
}catch(Exception e) {
System.out.println("Bir hata oluştu =>"+e.toString());
}finally {
System.out.println("Program sonlandı");
} } }
Throw ile Hata Mesajı Fırlatılması
import java.util.Scanner;
public class AritmetikIslem {
static boolean notKontrol(double x) { return(x>=0 && x<=100);
}
public static void main(String[] args) { Scanner giris=new Scanner(System.in);
System.out.print("Not giriniz:");
double not = giris.nextDouble();
if(!notKontrol(not))
Rastgele Sayı Üretme
import java.util.Random;
public class SayiUret {
public static void main(String[] args) { Random rand = new Random();
for(int i=0;i<10;i++) {
double sayi = rand.nextDouble();
System.out.println(sayi);
} }
}
sayi=Math.round(sayi*100.0)/100.0;
Rastgele Sayı Üretme
import java.util.Random;
public class SayiUret {
public static void main(String[] args) { Random rand = new Random();
//1 ile 49 arasında 6 sayı üretiniz for(int i=1;i<=6;i++) {
int s1= rand.nextInt(49)+1;
System.out.println(s1);
} }
Ratsgele Not Üretip Ortalamasını Bulma
import java.util.Random;
public class NotUretme {
public static void main(String[] args) { int rnot;
double ortalama=0, toplam=0;
Random rastgele = new Random();
for(int i=1;i<=20;i++) {
rnot =rastgele.nextInt(100)+1;
toplam+=rnot;
System.out.println(rnot);
}
ortalama=toplam/20;
System.out.println("Ortalama="+ortalama);
//Soru: Ortalamanın altında üretilen
Dosyaya Yazma
import java.io.FileWriter;
import java.io.IOException;
public class DosyaIslemleri {
public static void main(String[] args) throws IOException { String sehir="İstanbul";
FileWriter dosya=new FileWriter("cikis.txt");
for (int i = 0; i < sehir.length(); i++) dosya.write(sehir.charAt(i));
Dosyadan Okuma
import java.io.*;
public class DosyaIslemleri {
public static void main(String[] args) throws IOException { int ch;
FileReader dosya=null;
try {
dosya = new FileReader("cikis.txt");
}
catch (FileNotFoundException fe) {
System.out.println("Dosya bulunumadı");
}
// Dosya sonuna kadar oku while ((ch=dosya.read())!=-1)
System.out.print((char)ch);
Kaynaklar
●
Oracle Exceptions,
https://docs.oracle.com/javase/tutorial/essential/exceptions/
●
How to Java, «Deitel & Deitel», Pearson Yayınevi.
Sorularınız
•