第七章 例外处理(新).ppt

第七章 例外处理(新)

* 自定义异常类说明 用户定义的异常同样要用try-catch捕获,但必须由用户自己抛出异常。 例如: throw new MyException() * 自定义异常例子 class Exception_exp{ public static void main(String[] args){ try { System.out.println(2+3=+add(2,3)); System.out.println(-8+10=+add(-8,10));} catch (Exception e) { System.out.println(Exception is +e);} } static int add(int n,int m) throws UserException { if (n0|| m0) throw new UserException(); return n+m; }} class UserException extends Exception{ UserException(){super(数据为负数);}} 我们都希望自己的程序不包含任何错误,也都希望我们的程序要访问的资源总是可用。然而现实的情况可能和所期望

文档评论(0)

1亿VIP精品文档

相关文档