A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Queen123 中级黑马   /  2014-5-27 07:38  /  2542 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

异常:就是程序在运行时出现不正常情况。
异常由来:问题也是现实生活中一个具体的事物,也可以通过java的类的形式进行描述。并封装成对象。
                        其实就是java对不正常情况进行描述后的对象体现。

对于问题的划分:两种:一种是严重的问题,一种非严重的问题。

对于严重的,java通过Error类进行描述。
        对于Error一般不编写针对性的代码对其进行处理。

对与非严重的,java通过Exception类进行描述。
        对于Exception可以使用针对性的处理方式进行处理。

无论Error或者Exception都具有一些共性内容。
比如:不正常情况的信息,引发原因等。

Throwable
        |--Error
        |--Exception




2,异常的处理

java 提供了特有的语句进行处理。
try
{
        需要被检测的代码;
}
catch(异常类 变量)
{
        处理异常的代码;(处理方式)
}
finally
{
        一定会执行的语句;
}


3,对捕获到的异常对象进行常见方法操作。
        String getMessage():获取异常信息。


在函数上声明异常。
便于提高安全性,让调用出进行处理。不处理编译失败。



对多异常的处理。

1,声明异常时,建议声明更为具体的异常。这样处理的可以更具体。
2,对方声明几个异常,就对应有几个catch块。不要定义多余的catch块。
        如果多个catch块中的异常出现继承关系,父类异常catch块放在最下面。


建立在进行catch处理时,catch中一定要定义具体处理方式。
不要简单定义一句 e.printStackTrace(),
也不要简单的就书写一条输出语句。


*/

class Demo
{
        int div(int a,int b)throws ArithmeticException,ArrayIndexOutOfBoundsException//在功能上通过throws的关键字声明了该功能有可能会出现问题。
        {

                int[] arr = new int[a];

                System.out.println(arr[4]);

                return a/b;
        }
}


class  ExceptionDemo2
{
        public static void main(String[] args) //throws Exception
        {
                Demo d = new Demo();
                try
                {
                        int x = d.div(5,0);
                        System.out.println("x="+x);
                }
               
                catch(Exception e)
                {
                        System.out.println("hahah:"+e.toString());
                }
                catch (ArithmeticException e)
                {
                        System.out.println(e.toString());
                        System.out.println("被零除了!!");

                }
                catch (ArrayIndexOutOfBoundsException e)
                {
                        System.out.println(e.toString());
                        System.out.println("角标越界啦!!");
                }
               
                /**/
               

                System.out.println("over");

        }
}
Exceptoin中有一个特殊的子类异常RuntimeException 运行时异常。

如果在函数内容抛出该异常,函数上可以不用声明,编译一样通过。

如果在函数上声明了该异常。调用者可以不用进行处理。编译一样通过;

之所以不用在函数声明,是因为不需要让调用者处理。
当该异常发生,希望程序停止。因为在运行时,出现了无法继续运算的情况,希望停止程序后,
对代码进行修正。




自定义异常时:如果该异常的发生,无法在继续进行运算,
就让自定义异常继承RuntimeException。


对于异常分两种:
1,编译时被检测的异常。
       
2,编译时不被检测的异常(运行时异常。RuntimeException以及其子类)

*/

class FuShuException extends RuntimeException
{
        FuShuException(String msg)
        {
                super(msg);
        }
}
class Demo
{
        int div(int a,int b)throws Exception//throws ArithmeticException
        {
               
                if(b<0)
                        throw new Exception("出现了除数为负数了");
                if(b==0)
                        throw new ArithmeticException("被零除啦");
                return a/b;
        }
}

class ExceptionDemo4
{
        public static void main(String[] args)
        {
               
                Demo d = new Demo();
               
                int x = d.div(4,-9);
                System.out.println("x="+x);               
               
                System.out.println("over");
        }
}

/*
class Person
{
        public void checkName(String name)
        {
               
                //if(name.equals("lisi"))//NullPointerException
                if("lisi".equals(name))//if(name!=null && name.equals("lisi"))
                        System.out.println("YES");
                else
                        System.out.println("no");
        }
}

main()
{
        Person p = new Person();
        p.checkName(null);
}

2 个回复

倒序浏览
欢迎分享!
回复 使用道具 举报
Throwable
      |--Error   严重的错误
      |--Exception   异常,可以处理的不严重的情况
            |==RuntimeException 运行时异常  不需要被处理的异常

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马