java第十六天-异常体系

异常体系
a.异常:是在运行时期发生的不正常情况
b.Throwable 类是 Java 语言中所有错误或异常的超类;它有两个子类:Error和Exception;
c.Exception又分为两类:一种是CheckedException,一种是UncheckedException.这两种Exception的区别主要是CheckedException
需要用try…catch…显示的捕获,而UncheckedException不需要捕获。通常UncheckedException又叫做RuntimeException。
情况1:
String name=”“;
try{
//报错
}
catch (ArithmeticException e)
{
e.printStackTrace();//1
name=”323”;//2
return name;//5
}
finally
{
System.out.println(“Welcome”);//3
name=”323232323”;//4
}
return name;//不执行
情况2:
try{
//报错
}
catch (ArithmeticException e)
{
e.printStackTrace();//1
name=”323”;//2
return name;//3,执行了
}
finally
{
System.out.println(“Welcome”);//4
name=”323232323”;//5
return name;//6 执行了
}
情况3:
一个try后面可以跟多个catch,但不管多少个,最多只会有一个catch块被执行。
d.异常处理方法
对于非运行时异常(checked exception),必须要对其进行处理,否则无法通过编译。
1.使用try..catch..finally进行捕获
public class ExceptionTest2
{
public void method() throws Exception // 将异常抛出,由调用这个方法的方法去处理这个异常,如果main方法也将异常抛出,则交给Java虚拟机来处理
{
System.out.println(“Hello World”);

                        // 抛出异常
                        throw new Exception();
                    }

                    public static void main(String[] args)
                    {
                        ExceptionTest2 test = new ExceptionTest2();

                        try
                        {
                            test.method();
                        }
                        catch (Exception e)
                        {
                            e.printStackTrace();
                        }
                        finally
                        {
                            System.out.println("Welcome");
                        }
                    }
                }
            2.在产生异常的方法声明后面写上throws 某一个Exception类型,如throws  
              Exception,将异常抛出到外面一层去.
              public class ExceptionTest2
                {
                    public void method() throws Exception // 将异常抛出,由调用这个方法的方法去处理这个异常,如果main方法也将异常抛出,则交给Java虚拟机来处理
                    {
                        System.out.println("Hello World");

                        // 抛出异常
                        throw new Exception();
                    }
                    public static void main(String[] args) throws Exception // main方法选择将异常继续抛出
                    {
                        ExceptionTest2 test = new ExceptionTest2();

                        test.method(); // main方法需要对异常进行处理

                        // 执行结果:
                        // Hello World
                        // Exception in thread "main" java.lang.Exception
                        // at com.learnjava.exception.ExceptionTest2.method(ExceptionTest2.java:10)
                        // at com.learnjava.exception.ExceptionTest2.main(ExceptionTest2.java:17)
                    }

                }
            3.对于运行时异常(runtime exception),可以对其进行处理,也可以不处理。推荐不对运行时异常进行处理.
              自定义异常可以实现RuntimeException。
            4.自定义异常
                所谓自定义异常,通常就是定义一个类,去继承Exception类或者它的子类。因为异常必须直接或者间接地继承自Exception类
                    自定义一个异常类型:
                        public class MyException extends Exception
                        {
                            public MyException()
                            {
                                super();
                            }    
                            public MyException(String message)
                            {
                                super(message);
                            }
                        }
                        一种异常处理方式
                        public class ExceptionTest4
                        {

                            public void method(String str) throws MyException
                            {
                                if(null == str)
                                {
                                    throw new MyException("传入的字符串参数不能为null!");
                                }
                                else
                                {
                                    System.out.println(str);
                                }
                            }

                            public static void main(String[] args) throws MyException //异常处理方式1,不断向外抛出
                            {
                                ExceptionTest4 test = new ExceptionTest4();
                                test.method(null);
                            }
                            }
                            异常处理方式2
                            public static void main(String[] args)
                            {
                                //异常处理方式2,采用try...catch语句
                                try
                                {
                                    ExceptionTest4 test = new ExceptionTest4();
                                    test.method(null);

                                }
                                catch (MyException e)
                                {
                                    e.printStackTrace();
                                }    
                                finally
                                {
                                    System.out.println("程序处理完毕");
                                }

                            }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值