Exception 的三个方法
- package com.liuyanzhao;
- class ZeroException {
- int div(int a,int b) throws Exception{
- return a/b;
- }
- }
- public class Demo {
- public static void main(String[] args) {
- ZeroException z= new ZeroException();
- try {
- int x = z.div(4, 0);
- System.out.println(x);
- } catch( Exception e) {
- System.out.println(e.getMessage());/// by zero
- System.out.println(e.toString());//java.lang.ArithmeticException: / by zero
- e.printStackTrace();/*java.lang.ArithmeticException: / by zero
- at com.liuyanzhao.FushException.div(ExceptionDemo.java:7)
- at com.liuyanzhao.ExceptionDemo.main(ExceptionDemo.java:15)*/
- System.out.println("除0啦");//除0啦
- }
- }
- }
一个简单的try-catch语句块
- package com.liuyanzhao;
- class ZeroException{
- int div(int a,int b) throws Exception{
- return a/b;
- }
- }
- public class Demo {
- public static void main(String[] args) {
- ZeroExceptionz= new ZeroException();
- try {
- int x = z.div(4, 0);
- System.out.println(x);
- } catch( Exception e) {
- System.out.println(e.toString());//java.lang.ArithmeticException: / by zero
- }
- }
- }
ducking只是在踢皮球,方法和main都抛出
- package com.liuyanzhao;
- /*
- * 1、 div()已经抛掉了异常
- * 连main()抛掉了异常
- * 最后只剩下Java虚拟机,你知道这家伙对异常是没有什么责任感的
- * Java虚拟机只好死给你看
- * 2、 两者都躲避异常,因此没有人来处理
- * 但有抛掉了,所以可以通过编译
- * 3、 本例的div方法的内的异常是输入RuntimeException
- * 所以它后面的声明异常(throws Exception)是可有可无的,可以省略
- * 但是main方法里要么try/catch处理,要么throws抛掉
- */
- class ZeroException{
- int div(int a,int b) throws Exception {
- return a/b ;
- }
- }
- public class Demo {
- public static void main(String[] args) throws Exception {
- ZeroException z= new ZeroException();
- int x = z.div(4, );
- System.out.println(x);
- /* 正常编译,不美观的异常
- * Exception in thread "main" java.lang.ArithmeticException: / by zero
- at com.liuyanzhao.FushException.div(Demo.java:4)
- at com.liuyanzhao.Demo.main(Demo.java:11)
- */
- }
- }
自定义异常信息和多个catch
- package com.liuyanzhao;
- /*
- * 1、throws是在方法声明后面
- * throw是在方法里面
- * 2、自定义异常信息,需要继承父类Exception
- * 3、有多个catch,需要从小到大
- * 4、异常也是多态的
- * 别忘了异常也是对象
- * 除了可以被抛出外,也没什么特别的
- * 5、可以用super来处理所有异常
- * 并不代表就应该这样做
- */
- class FushException extends Exception{
- private String message;
- public String getMessage() {
- return message;
- }
- public FushException(String message) {
- this.message = message;
- //super(message);
- }
- int div(int a,int b) throws FushException {
- if(b<0)
- throw new FushException("除数不能为负数");
- return a/b ;
- }
- }
- public class Demo {
- public static void main(String[] args) {
- try {
- FushException f = new FushException("");
- int x = f.div(4,-1);
- System.out.println(x);
- } catch (FushException e) {
- System.out.println(e.getMessage());
- } catch(ArithmeticException e) {
- System.out.println(e.getMessage());
- } catch(Exception e) {
- System.out.println(e.getMessage());
- }
- }
- }
异常处理规则
①catch和finally不能没有try
- public static void main(String[] args) {
- //try {
- FushException f = new FushException();
- int x = f.div(4, -1);
- System.out.println(x);
- //}
- catch (FushException e) {
- System.out.println(e.getMessage());
- }
- }
无法通过编译
②try与catch之间不能有程序
- public static void main(String[] args) {
- try {
- FushException f = new FushException();
- int x = f.div(4, -1);
- System.out.println(x);
- }
- int a = 1;//中间不能插入代码
- catch (FushException e) {
- System.out.println(e.getMessage());
- }
- }
无法通过编译
③try一定要有catch或finally
- public static void main(String[] args) {
- try{
- FushException f = new FushException();
- int x = f.div(4,-1);
- System.out.println(x);
- }
- // catch(FushException e) {
- // System.out.println(e.getMessage());
- // }
- finally {
- //清理
- }
- }
这个是合法的,可以通过编译。catch和finally至少需要有一个,但还是要注意 第④项
④自带有finally的try必须要声明异常
- public static void main(String[] args) throws FushException{
- try{
- FushException f = new FushException();
- int x = f.div(4,-1);
- System.out.println(x);
- }
- finally {
- //清理
- }
- }
main必须要throws 异常,可以编译
本文地址:http://liuyanzhao.com/3919.html
转载请注明
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏