Java – Applet – Throw Exceptions (Custom Exceptions) Sayı sıfıra bolunmez kuralı

Yorum Yok

import javax.swing.JApplet;
import javax.swing.JOptionPane;
import java.awt.Graphics;

public class Bolunmez extends JApplet
{
 private double s1;
 private double s2;
 private double bolum;
 
  static double MyException(double s1,double s2)
 throws Exception
 {
 
  if(s2==0)
  {
   throw new Exception(“Sifira bolunmez”);
  }
  else
  {
   return s1/s2;
  }
 }
 
 public void init()
 { 
  String sayi;
  
  sayi = JOptionPane.showInputDialog(“İlk sayiyi giriniz”);
  s1 = Integer.parseInt(sayi);
  
  sayi = JOptionPane.showInputDialog(“İlk sayiyi giriniz”);
  s2= Integer.parseInt(sayi);
  
 }
 
 public void paint( Graphics g )
 {
  super.paint(g);
   try{
   double bolum=MyException(s1,s2);
   JOptionPane.showMessageDialog(null,String.format(“Bolum : %.2f\n”,bolum));
   g.drawString(String.format(“Bolum sonucu : %.2f” ,bolum),25,30);
  }
  catch(Exception ex)
  {
   JOptionPane.showMessageDialog(null,ex.toString());
  }
 }
}

Cevapla