Электронная библиотека книг Александра Фролова и Григория Фролова.
Shop2You.ru Создайте свой интернет-магазин
Библиотека
Братьев
Фроловых
[Назад]

Исходный текст программы throwDemo.java

Оглавление

Назад

// =======================================
// throwDemo.java
//
// (C) Alexandr Frolov, 1998
// E-mail: frolov@glasnet.ru
// Web:    http://www.glasnet.ru/~frolov
// =======================================
public class throwDemo 
{
    public static void main(String args[]) 
    {
      try
      {
	MyPoint mp = new MyPoint(0, -1);
      }
      catch(NegativePointCoordinateEx e)
      {
        System.out.println("\n" + e.toString()); 
        System.out.println("Point: (" +
          e.x + "," + e.y + ")"); 
      }
      System.out.println("\n" ); 
    }
}

class MyPoint
{
  private int m_x=0;
  private int m_y=0;
  
  MyPoint(int ix, int iy)
    throws NegativePointCoordinateEx
  {
    if((ix == 0) | (iy == 0))
    {
      throw 
    new NegativePointCoordinateEx(ix, iy, this);
    }
    m_x = ix;
    m_y = iy;
    
  }
  
  public void setPoint(int ix, int iy)
    throws NegativePointCoordinateEx
  {
    if((ix == 0) | (iy == 0))
    {
      throw 
     new NegativePointCoordinateEx(ix, iy, this);
    }
    m_x = ix;
    m_y = iy;
  }
  
  public void printPoint()
  {
    System.out.println("Point: (" +
      m_x + "," + m_y + ")"); 
  }
}

class NegativePointCoordinateEx
  extends Exception
{
  public int x;
  public int y;
    
  NegativePointCoordinateEx(int ix,
    int iy, Object obj)
  {
    super("Negative Point Coordinate: ("
     + ix + "," + iy + ")");
    x = ix;
    y = iy;
  }
}

Назад

[Назад]