public class UserException
    extends Exception
{
    private String name;
    UserException()
    {
        super();
    }    UserException(String name)
    {
        super(name);
        this.name = name;
    }    public String toString()
    {
        return name + super.toString();
    }}

解决方案 »

  1.   

    public class TestUserException
    {
        public TestUserException()
        {
        }
        public static void main(String[] args)
        {
            int a=10,b=-10;
            try
            {
                if (a>b)
                    throw new UserException("a is bigger than b!");
            }
            catch(UserException ue)
            {
                ue.printStackTrace();
            }
        }
    }
      

  2.   

    try{
      ...........
    }catch( Exception e ){
      throw new UserException();
    }
      

  3.   

    public void testException throws UserException{
        try{
        }catch(Exception e){
            throw new UserException();
        }
    }
    如果需要向上一级抛出异常,就需要在方法名用throws来定义要向调用者抛出的异常才行