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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Tauruszzy 中级黑马   /  2015-5-9 13:01  /  284 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

class Demo
{
        int div(int a,int b)
        {
                return a/b;
        }
}

class ExceptionDemo
{
        public static void main(String[] args)
        {
                Demo d=new Demo();
                int x=d.div(4,0);//这里产生异常后,程序停止运行,假如其后又很多代码,因为这一个错误
                                         //导致后面的代码都无法运行是十分让人郁闷的,所以我们有必要对程序进行处
                                        //理。这里引入异常的第二种情况:Exception。它是不严重的情况,可以被
                                       //处理。这里介绍新的知识try catch.见虚线下内容
                System.out.println("x="+x);
                System.out.println("");
                System.out.println("");
                System.out.println("");

        }
}
---------------------------------------------------------
/*
模板
*/
try
{
需要检测的代码;
}
catch(异常类 变量)
{
处理异常的代码
}
finally
{
一定会执行的语句;
}
---------------------------------------------------------
/*
流程
*/
class Demo
{
        int div(int a,int b)
        {
                return a/b;//第三步:进行运算,引发问题,即java虚拟机识别的算术问题,
                                //产生ArithmeticException,并把它封抓装成对象new ArithmeticException();
                                   //把该对象丢给调用它的主函数,并返回给第二步那里。
        }
}

class ExceptionDemo
{
        public static void main(String[] args)
        {
                Demo d=new Demo();//第一步:建立对象
                try//第5步:检测到了异常,丢给catch
                {
                        int x=d.div(4,0);//第二步:把4和0传给a,b  第4步:接受new ArithmeticException();
                                                 //对象
                    System.out.println("x="+x);
                }
                catch (Exception e)//第6步:Exception e=new ArithmeticException();注意:多态
                {
                        System.out.println("除零啦");//第7步:执行代码,代表问题已被处理
                }
               
                System.out.println("谢谢");//第8步
                System.out.println("嘿嘿");//第9步

        }
}
---------------------------------------------------------
/*
对捕获的异常对象进行操作
*/
class Demo
{
        int div(int a,int b)
        {
                return a/b;
        }
}

class ExceptionDemo
{
        public static void main(String[] args)
        {
                Demo d=new Demo();//第一步:建立对象
                try//第5步:检测到了异常,丢给catch
                {
                        int x=d.div(4,0);//
                    System.out.println("x="+x);
                }
                catch (Exception e)//第6步:Exception e=new ArithmeticException();
                {
                        System.out.println("除零啦");
                        System.out.println(e.getMessage());//异常信息 /by zero
                        System.out.println(e.toString());//打印异常名称和异常信息
                        e.printStackTrace();//异常名称,异常信息,异常出现的位置
                                                        //其实jvm默认的处理机制就是在调用这个方法
                                                        //打印异常在堆栈的跟踪信息
                }
               
                System.out.println("谢谢");//第8步
                System.out.println("嘿嘿");//第9步

        }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马