Java – Applet – Throw Exceptions (Custom Exceptions) Array

Yorum Yok

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


public class Array extends JApplet
{
  private int number;
  private int toplam;
  public int i;
  private int[] arry = new int[2];
  static int sayilar (int sayi) throws Exception
  {
   if (sayi<0)
   {
    throw new Exception(“Sayi sifirdan kucuk olamaz”);
   }
   return sayi;
  }
  public void init()
  {
  String isim;
 
 for(i=0;i<2;i++)
 {
 isim = JOptionPane.showInputDialog(“ilk sayiyi gir” );
 number = Integer.parseInt(isim);
  try
  {
   arry[i]=sayilar(number);
   toplam +=arry[i];
  }
  catch (Exception ex)
  { i–;
   JOptionPane.showMessageDialog(null,ex.toString());
  }
  }
  }
  
  public void paint( Graphics g )
{
int a=25;
super.paint( g ); // call superclass version of method paint
  try
  {
   for (i=0;i<arry.length;i++)
   {
    g.drawString (String.format(“%d. sayi : %d\n”,i+1,arry[i]),25,a+=25);
   }
   g.drawString(“Toplam ” + toplam,25,100);
  }
  catch(Exception e)
  {
   JOptionPane.showMessageDialog(null,e.toString());
  }
  
}
}

Cevapla