- public class TestRuntimeException {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- //MyException me = new MyException("除零了");
- Demo d = new Demo();
- try{
- System.out.println(d.div(5,1));
-
- }catch(MyException e){
- e.toString();
- }
- }
- }
- class MyException extends Exception{
-
- public MyException(String msg){
- super(msg);
- }
-
- }
- class Demo{
-
- public int div(int a,int b)throws MyException{
- if(b==0)
- throw new MyException("除零");
- return a/b;
- }
- }
复制代码 |
|