- class SelfException extends Exception
- {
- SelfException(String msg)
- {
- super(msg);
- }
- }
- class ExceptionDemo
- {
- public int div(int a,int b)throws SelfException
- {
- if (b<0)
- {
- throw new SelfException("除数出现负数不和要求");
- }
- return a/b;
- }
- }
- class Demo
- {
- public static void main(String[] args)
- {
- ExceptionDemo e = new ExceptionDemo();
- try
- {
- int x = e.div(4,1);
- System.out.println(x);
- }
- catch (SelfException s)
- {
- System.out.println(s.toString());
- }
- System.out.println("Hello World!");
- }
- }
复制代码 |