Java从入门到精通 - 异常处理

public class Baulk {
 /*public static void main(String args [])
 {
  //int result =3/0;
  //System.out.println(result);  //by zero.
  try
  {
   String str = "lili";
   System.out.println(str + "年龄是:");
   int age = Integer.parseInt("10l");
   System.out.println(age);
  }catch(Exception ee)
  {
   ee.getMessage();
  }
  System.out.println("pagram over.");
 }*/
 static void pop() throws NegativeArraySizeException
 {
  int [] arr = new int[-3];
 }
 public static void main(String [] args)
 {
  try
  {
   Tran tran = new Tran();
   int result = tran.avg(102,150);
   System.out.println(result);
  }catch(MyException e)
  {
   System.out.println(e);
  }
  //shu zu exception test ;
  try
  {
   pop();
  }catch(NegativeArraySizeException e)
  
  {
   System.out.println("pop() method exception");
  }
 }
}
class MyException extends Exception
{
 public MyException(String ErrorMessage)
 {
  super(ErrorMessage);
 }
}
class Tran
{
 static int avg(int number1,int number2) throws MyException
 {
  if(number1<0 || number2 < 0)
  {
   throw new MyException("不可以使用负数。");
  }
  if(number1>100 || number2 >100)
  {
   throw new MyException("数值太大了。");
  }
  return (number1 + number2)/2;
 }
 
}

你可能感兴趣的:(Java从入门到精通 - 异常处理)