JavaSE 自定义Exception

来源:互联网 发布:帝国cms灵动标签大全 编辑:程序博客网 时间:2024/05/20 15:38

简单练习自定义异常 &&抛出异常

package com.exception.sysu;public class MyException extends Exception{public MyException(){super();}public MyException(String string){super(string);}}

package com.exception.sysu;public class UsingException {public String function(String str) throws Exception{if(str.equals("hello")){throw new MyException("something wrong!");}return "success!";}public static void main(String[] args) {String str=null;try{UsingException using=new UsingException();str=using.function("hello");}catch (Exception e) {e.printStackTrace();}finally{System.out.println(str);}}}

结果:


0 0
原创粉丝点击