CXYVIP官网源码交易平台_网站源码_商城源码_小程序源码平台-丞旭猿论坛
CXYVIP官网源码交易平台_网站源码_商城源码_小程序源码平台-丞旭猿论坛
CXYVIP官网源码交易平台_网站源码_商城源码_小程序源码平台-丞旭猿论坛

Java,异常处理,自定义异常,异常作为业务处理、异常的应用-源码交易平台丞旭猿

Java异常

1、Java的异常是面向对象的。

一个Java的Exception是一个描述异常情况的对象。当出现异常情况时,一个Exception对象就产生了,并放到异常的成员函数里。

2、Java的异常处理是通过5个关键词来实现的:try,catch,throw,throws和finally。在Java语言的错误处理结构由try,catch,finally三个块组成。其中try块存放将可能发生异常的Java语言,并管理相关的异常指针;catch块紧跟在try块后面,用来激发被捕获的异常;finally块包含清除程序没有释放的资源,句柄等。不管try块中的代码如何退出,都将执行finally块。

3、try 包含可能发生异常的代码块。

4、catch 捕获异常。

5、finally 不管有无异常发生,最后都执行。

6、throw 抛出异常。

7、throws 声明抛出的异常类型。多态性是指允许不同类的对象对同一消息作出响应。

8、异常包含:受控异常和运行时异常(RuntimeException)。

受控异常和非受控异常

受控异常:Checked Exception,这类异常必须写try{},catch{},或者throw抛出,否则编译通不过。

非受控异常:Unchecked Exception,这类异常也叫做运行时异常(与非受控异常 字数相等),这类异常不需要try{},catch{},也不需要throw抛出,编译能通过。

为什么要使用非受控异常?

为了简化代码。试想一下,如果所有可能出现异常的地方(比如访问数组元素可能会越界、调用对象方法,对象可能为null),都写try{},catch{},或者throw 抛出,那么代码肯定冗余的不成样子了。也就是说,采用非受控异常(运行时异常)可以减少代码的污染。

import java.io.FileInputStream;
import java.io.IOException;/**
 * Java异常处理
 */publicclassExceptionDemo{/**
     * @param args
     */publicstaticvoidmain(String[] args){// 受控异常(非运行时异常),需要声明抛出;// 运行时异常(RuntimeException),不需要声明抛出;try{
            testException("");
        }catch(IOException e) {
            e.printStackTrace();
        }
        testRuntimeException(1);
        testRuntimeException(0);// 无异常,执行顺序: try -> finally// 发生异常,执行顺序: try -> catch -> finally}/**
     * @param s
     * @throws IOException
     */publicstaticvoidtestException(String s) throws IOException{try{
            System.out.println("try start ... ");newFileInputStream(s);
            System.out.println("try end ... ");
        }catch(IOException e) {
            System.out.println("catch IOException ... ");throwe;
        }finally{
            System.out.println("finally ... ");
        }
    }/**
     * @param i
     */publicstaticvoidtestRuntimeException(inti){try{
            System.out.println("try start ... ");
            System.out.println(2/ i);
            System.out.println("try end ... ");
        }catch(RuntimeException e) {
            System.out.println("catch RuntimeException ... ");thrownewRuntimeException(e);
        }finally{
            System.out.println("finally ... ");
        }
    }

}

自定义异常

自定义异常类只需从Exception类或者它的子类派生一个子类即可,自定义异常类如果继承Exception类,则为:受检查异常,必须对其进行处理;如果不想处理,可以让自定义异常类继承运行时异常:RuntimeException类。

publicclassTestExceptionextendsException{privatestaticfinallongserialVersionUID =2039023617341817264L;publicTestException(){

    }publicTestException(String msg){super(msg);
    }publicTestException(Throwable target){super(target);
    }publicTestException(String msg, Throwable target){super(msg, target);
    }

}
publicclassTestRuntimeExceptionextendsRuntimeException{privatestaticfinallongserialVersionUID =2039023617341817264L;publicTestRuntimeException(){

    }publicTestRuntimeException(String msg){super(msg);
    }publicTestRuntimeException(Throwable target){super(target);
    }publicTestRuntimeException(String msg, Throwable target){super(msg, target);
    }

}
/**
 * Java自定义异常
 */publicclassMyExceptionTest{/**
     * @param args
     */publicstaticvoidmain(String[] args){try{
            test("");
        }catch(TestException e) {
            e.printStackTrace();
        }
        test(1);
        test(0);
    }/**
     * @param s
     * @throws TestException
     */publicstaticvoidtest(String s) throws TestException{try{newFileInputStream(s);
        }catch(Exception e) {thrownewTestException("出错.....", e);
        }
    }/**
     * @param i
     */publicstaticvoidtest(inti){if(i ==0) {thrownewTestRuntimeException("出错.....");
        }
        System.out.println(2/ i);
    }

}

异常作为业务处理

publicclassExceptionTest{/**
     * @param args
     */publicstaticvoidmain(String[] args){// 处理方法一if(isNull("")) {
            System.out.println("字符串为空。");
        }// 处理方法二try{
            checkNull("");
        }catch(RuntimeException e) {
            System.out.println("字符串为空, "+ e.getMessage());
        }try{
            checkNull2("");
        }catch(Exception e) {
            System.out.println("字符串为空, 错误代码: "+ e.getMessage());
        }try{
            checkNull3(null);
        }catch(TestException e) {
            System.out.println("字符串为空, 错误代码: "+ e.getMessage());
        }
    }/**
     * @param str
     * @return
     */privatestaticbooleanisNull(String str){if(str ==null) {returntrue;
        }if("".equals(str)) {returntrue;
        }returnfalse;
    }/**
     * @param str
     */privatestaticvoidcheckNull(String str){if(str ==null) {thrownewRuntimeException("param is null");
        }if("".equals(str)) {thrownewRuntimeException("param is empty");
        }
    }/**
     * @param str
     * @throws Exception
     */privatestaticvoidcheckNull2(String str) throws Exception{if(str ==null) {thrownewException("01");
        }if("".equals(str)) {thrownewException("02");
        }
    }/**
     * @param str
     * @throws TestException
     */privatestaticvoidcheckNull3(String str) throws TestException{if(str ==null) {thrownewTestException("01");
        }if("".equals(str)) {thrownewTestException("02");
        }
    }

}

声明:本文部分素材转载自互联网,如有侵权立即删除 。

© 版权声明
THE END
喜欢就支持一下吧
点赞0赞赏 分享
相关推荐
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容