文章导读:Java异常 1、Java的异常是面向对象的。 一个Java的Exception是一个描述异常情况的对象。当出现异常情况时,一个Exception对象就产生了,并放到异常的成员函数里。 2、Java的异常处理是通过5个关键词来实现的:try……各位看官请向下阅读:
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异常处理 */public class ExceptionDemo { /** * @param args */ public static void main(String[] args) { // 受控异常(非运行时异常),需要声明抛出; // 运行时异常(RuntimeException),不需要声明抛出; try { testException(""); } catch (IOException e) { e.printStackTrace(); } testRuntimeException(1); testRuntimeException(0); // 无异常,执行顺序: try -> finally // 发生异常,执行顺序: try -> catch -> finally } /** * @param s * @throws IOException */ public static void testException(String s) throws IOException { try { System.out.println("try start ... "); new FileInputStream(s); System.out.println("try end ... "); } catch (IOException e) { System.out.println("catch IOException ... "); throw e; } finally { System.out.println("finally ... "); } } /** * @param i */ public static void testRuntimeException(int i) { try { System.out.println("try start ... "); System.out.println(2 / i); System.out.println("try end ... "); } catch (RuntimeException e) { System.out.println("catch RuntimeException ... "); throw new RuntimeException(e); } finally { System.out.println("finally ... "); } }}
自定义异常
自定义异常类只需从Exception类或者它的子类派生一个子类即可,自定义异常类如果继承Exception类,则为:受检查异常,必须对其进行处理;如果不想处理,可以让自定义异常类继承运行时异常:RuntimeException类。
public class TestException extends Exception { private static final long serialVersionUID = 2039023617341817264L; public TestException() { } public TestException(String msg) { super(msg); } public TestException(Throwable target) { super(target); } public TestException(String msg, Throwable target) { super(msg, target); }}
public class TestRuntimeException extends RuntimeException { private static final long serialVersionUID = 2039023617341817264L; public TestRuntimeException() { } public TestRuntimeException(String msg) { super(msg); } public TestRuntimeException(Throwable target) { super(target); } public TestRuntimeException(String msg, Throwable target) { super(msg, target); }}
/** * Java自定义异常 */public class MyExceptionTest { /** * @param args */ public static void main(String[] args) { try { test(""); } catch (TestException e) { e.printStackTrace(); } test(1); test(0); } /** * @param s * @throws TestException */ public static void test(String s) throws TestException { try { new FileInputStream(s); } catch (Exception e) { throw new TestException("出错.....", e); } } /** * @param i */ public static void test(int i) { if (i == 0) { throw new TestRuntimeException("出错....."); } System.out.println(2 / i); }}
异常作为业务处理
public class ExceptionTest { /** * @param args */ public static void main(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 */ private static boolean isNull(String str) { if (str == null) { return true; } if ("".equals(str)) { return true; } return false; } /** * @param str */ private static void checkNull(String str) { if (str == null) { throw new RuntimeException("param is null"); } if ("".equals(str)) { throw new RuntimeException("param is empty"); } } /** * @param str * @throws Exception */ private static void checkNull2(String str) throws Exception { if (str == null) { throw new Exception("01"); } if ("".equals(str)) { throw new Exception("02"); } } /** * @param str * @throws TestException */ private static void checkNull3(String str) throws TestException { if (str == null) { throw new TestException("01"); } if ("".equals(str)) { throw new TestException("02"); } }}
以上内容由优质教程资源合作伙伴 “鲸鱼办公” 整理编辑,如果对您有帮助欢迎转发分享!
你可能对这些文章感兴趣:- Excel表格十大函数(Excel表格函数公式出现错误怎么办?)
- 对不起!忘了告诉你如何激活win10LTSC版,比windows7流畅干净
- Mac新手使用技巧——Mac音量调节
- 新入手的iPhone 13,记得提前打开这5个功能,让手机更好用
- iPhone状态栏中怎么显示图标 状态栏爱心等图标设置方法
- iphone应用图标大小要怎么调整?(iPhone应用图标大小)
- 有办法了!批量删除多个Word页眉页脚
- Excel常用函数知识:使用RANK.EQ或RANK.AVG函数进行排名计算
- RANKIF?Excel中有这个函数吗?
- 手机与和U盘的连接及文件操作(手机直接连接u盘为什么不能互传文件)
本文地址:https://logohe.com/470193.html ,如需转载请文章来源:办公资源网
声明:本站所有文章均为网络资源收集于及用户投稿,只做学习和交流使用,版权归原作者所有,请在下载后24小时之内自觉删除。如若本站内容侵犯了原著者的合法权益,请联系站长484405847@qq.com删除,我们将及时处理!