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");
}
}
}
声明:本文部分素材转载自互联网,如有侵权立即删除 。
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 精力有限,不少源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别
丞旭猿论坛
暂无评论内容