我看别人的面经中有一道题目就问到了Exception,即java的异常处理的,我曾经也学了java的异常处理,可是我查了下,看了别人的博客关于写的exception异常处理。我发现,自己学的不坚固,仅仅学到了一点皮毛而已,在看了那么多博客和资料后。我做下总结吧,不然我大脑都混乱了。

java的中异常祖先是Throwable,Throwable的直接子类是Exception和Error。

Error通过单词我们就知道,是错误的意思。这样的错误通常是jvm执行产生的错误,出现这样的错误,我们的程序不能解决,比方内存溢出oom,堆溢出等。这样的错误。我们不必处理,直接让jvm抛出报错,我们没办法解决就无论了。

Exception中文意思是异常,那么Exception又分为检查性异常和非检查性异常。比方RuntimeException类及子类就是非检查性异处,表示执行时出现的异常,有数组越界,空指针异常。我们也能够不进行处理,让jvm自己抛出异常,当然假设我们能够预见这样的异常的话。最好在程序中进行推断检查,程序写健壮些。有的这样的异常就能够避免了。effect java有这样的处理的推荐,详细的能够看看这本书。

Exception另一类是检查性异常,这是除RuntimeException类及子类外的Exception类和Exception类的其它子类。

检查性异常,必需要进行异常处理的或者抛出,否则编译器会报错。

异常处理一般用到以下几个keywordtry,catch。finally。throws,throw,以下我逐个做一个说明。

try:

对于可能会抛出异常的代码一般放在try{}代码块中,比方:

try {
  int   i=2/0;
t.test1();
//System.out.println("kkk"+st);
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("i will catch you....");
e.printStackTrace();
}

上面的代码2/0,肯定会抛出异常的。所以须要放在try{}块中,在try块中。假设哪一行抛出了异常,那么try块后面的代码将不再运行,将直接进入异常处理代码块。

catch:

catch(Excetion e){}这样的结构是用来处理。try代码块中抛出的异常的,能够有非常多的catch代码块,只是要注意的是,当有非常多catch代码块,而且catch里面的參数有父子关系的时候,catch里面的參数一定要是子类在前面,父类在后面。由于按catch代码块出现的先后顺序匹配抛出的异常,一旦抛出的异常是哪个catch里面參数的子类或同类,那么就能够被这个catch代码块进行处理。那么其它的catch代码块就算也符合这样的条件也不在进行处理了,假设有finally代码块的话就直接进入finally代码块中,没得则直接执行catch代码块后面的代码了。

finally:

finally代码块是异常处理中一定会运行的代码块,就算没得异常也会运行。有异常则进入catch代码块中处理后,在进入finally中处理。

在这里要注意一点,假如在try代码块中有return 语句。那么也是先运行finally里面的代码块后在运行return语句的。

还需注意一点的是。函数的返回值,是最后活动栈的栈顶的值。假设try进行return了,那么在finally中我也进行return。那么finally代码块里面的return的值在栈顶,所以会返回finally中return的值。有例如以下測试代码。

package com.wj.exception;

public class TestException3 {

	public String testException(){
try{
return "I am in try";
}finally{
return "I am in finally";
}
} public static void main(String[] args) {
// TODO Auto-generated method stub TestException3 te=new TestException3();
System.out.println(te.testException());
} }

执行结果是:

I am in finally

注意我这里是測试所以在finally中进行return了,一般不推荐在finally中进行return。

throws:

throws的使用方法是在      函数名(參数1,參数2.....)throws 异常类1,异常类2,异常类3.....{}

throws是表明这个函数可能会有异常产生,可是函数不进行异常处理,函数将向上抛异常,把异常处理交给函数的调用处进行处理。假设调用处。不进行处理则能够在进行向上抛。。

直到有地方进行try{}catch(){}异常处理,或者最后抛给jvm(不建议这么使用)。

package com.wj.exception;

public class TestException3 {

	public String testException() throws Exception{
/*try{
return "I am in try";
}finally{
return "I am in finally";
}*/
System.out.println("我不进行异常处理。我将异常向上抛。throws");
int i=2/0;
System.out.println("有异常我将得不到运行。。。。");
return "我是使用throws抛的异常";
} public static void main(String[] args) {
// TODO Auto-generated method stub TestException3 te=new TestException3();
try {
System.out.println(te.testException());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }

输出结果:

<span style="font-size:18px;">我不进行异常处理。我将异常向上抛,throws
java.lang.ArithmeticException: / by zero
at com.wj.exception.TestException3.testException(TestException3.java:13)
at com.wj.exception.TestException3.main(TestException3.java:24)
</span>

throw:

throw的作用是详细的抛出一个异常类对象。一般在方法里面使用。当在try代码块中使用的时候。直接进入catch代码块中进行处理。

比方:

package com.wj.exception;

public class TestException3 {

	public String testException(){
try{
throw new Exception("i is throw exception");
}catch(Exception e){
System.out.println("我处理throw抛出的异常");
}finally{
System.out.println("我运行finally代码块\n"); }
System.out.println("异常处理结束! ! !");
return "I am in finally"; } public static void main(String[] args) {
// TODO Auto-generated method stub TestException3 te=new TestException3(); System.out.println(te.testException()); } }

同意结果:

我处理throw抛出的异常

我运行finally代码块





异常处理结束!!



I am in finally

throw手动抛出的异常须要从里到到外查找处理的try代码块。没有处理话,就须要把方法后面加throws,把异常向上抛。看例如以下代码:

1

package com.wj.exception;

public class TestException3 {

	public String testException(){
try{
int i=3/0;
}catch(Exception e){
throw new Exception();
}finally{
System.out.println("我运行finally代码块\n"); }
//System.out.println("异常处理结束。。! ");
return "I am in finally"; } public static void main(String[] args) {
// TODO Auto-generated method stub TestException3 te=new TestException3(); System.out.println(te.testException()); } }

2

上面的代码在编译器里面会出现错误,提示我们使用throws抛出异常,或者把throw new Exception()进行因此处理。改成这种

<pre name="code" class="java">package com.wj.exception;

public class TestException3 {

	public String testException() {
try{
int i=3/0;
}catch(Exception e){
try {
throw new Exception();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}finally{
System.out.println("我运行finally代码块\n"); }
//System.out.println("异常处理结束!。!");
return "I am in finally"; } public static void main(String[] args) {
// TODO Auto-generated method stub TestException3 te=new TestException3(); System.out.println(te.testException()); } }


输出结果:

java.lang.Exception
at com.wj.exception.TestException3.testException(TestException3.java:11)
at com.wj.exception.TestException3.main(TestException3.java:32)
我运行finally代码块 I am in finally

3

或者说是这种:

<pre name="code" class="java">package com.wj.exception;

public class TestException3 {

	public String testException()  throws Exception{
try{
int i=3/0;
}catch(Exception e){ throw new Exception(); }finally{
System.out.println("我运行finally代码块\n");
}
//System.out.println("异常处理结束! !。");
return "I am in finally"; } public static void main(String[] args) {
// TODO Auto-generated method stub TestException3 te=new TestException3(); try {
System.out.println(te.testException());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } }

输出结果:


我运行finally代码块

java.lang.Exception
<span style="white-space:pre"> </span>at com.wj.exception.TestException3.testException(TestException3.java:11)
<span style="white-space:pre"> </span>at com.wj.exception.TestException3.main(TestException3.java:28)

4

这样改,非常easy,可是另一种更加奇葩的改法。能够使编译器不出错,例如以下:

package com.wj.exception;

public class TestException3 {

	public String testException()  {
try{
int i=3/0;
}catch(Exception e){ throw new Exception(); }finally{
System.out.println("我运行finally代码块\n");
return "I am in finally";
}
//System.out.println("异常处理结束! ! ! ");
//return "I am in finally"; } public static void main(String[] args) {
// TODO Auto-generated method stub TestException3 te=new TestException3(); System.out.println(te.testException()); } }

输出:

我运行finally代码块

I am in finally

发现没有第四个代码。本来是跑出了一个异常的,可是没有输出不论什么的异常信息。

并且把函数返回值改为void,在finally里面不使用return。则又会报错,这一点我不是非常明确了。难道在catch里面使用throw抛出的异常,在finally代码块里面使用return语句后,就被处理了嘛?要是被处理了怎么没得异常输出,还是输出的异常信息在栈里面被覆盖了?知道怎么回事的朋友,能够交流下。

看了上面那么多,你是否对java中的异常有一定理解了?

我在博客http://blog.csdn.net/hguisu/article/details/6155636里面看到一个样例,貌似比較经典。就借用下了,代码例如以下了:

package com.wj.exception;

public class TestException1 {

	boolean testEx() throws Exception {
boolean ret = true;
try {
ret = testEx1();
} catch (Exception e) {
System.out.println("testEx, catch exception");
ret = false;
throw e;
} finally {
System.out.println("testEx, finally; return value=" + ret);
return ret;
}
} boolean testEx1() throws Exception {
boolean ret = true;
try {
ret = testEx2();
if (!ret) {
return false;
}
System.out.println("testEx1, at the end of try");
return ret;
} catch (Exception e) {
System.out.println("testEx1, catch exception");
ret = false;
throw e;
} finally {
System.out.println("testEx1, finally; return value=" + ret);
return ret;
}
} boolean testEx2() throws Exception {
boolean ret = true;
try {
int b = 12;
int c;
for (int i = 2; i >= -2; i--) {
c = b / i;
System.out.println("i=" + i);
}
return true;
} catch (Exception e) {
System.out.println("testEx2, catch exception");
ret = false;
throw e;
} finally {
System.out.println("testEx2, finally; return value=" + ret);
return ret;
}
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub TestException1 testException1 = new TestException1();
try {
testException1.testEx();
} catch (Exception e) {
System.out.println("结束\n");
e.printStackTrace();
} } }

假设不看答案,你能说出执行结果嘛?你能够到自己的编译器中执行下看下结果,在进行debug下,看看是否和你想的一样了。

上面执行的结果例如以下:

i=2

i=1

testEx2, catch exception

testEx2, finally; return value=false

testEx1, finally; return value=false

testEx, finally; return value=false

我进行过debug。所以我清楚执行的流程,可是对于内部的机制,还有点疑点,就像我在4所说的。finally中的return 会覆盖throw抛出的异常一样。真的是这种嘛,假设是这种话。上面的代码就能分析出和执行一样的输出结果了,能够试试,一起交流下。

本文原创,转载请著明:http://blog.csdn.net/j903829182/article/details/39808307

java异常处理Exception的更多相关文章

  1. Java异常处理:如何写出“正确”但被编译器认为有语法错误的程序

    文章的标题看似自相矛盾,然而我在"正确"二字上打了引号.我们来看一个例子,关于Java异常处理(Exception Handling)的一些知识点. 看下面这段程序.方法pleas ...

  2. java 异常处理 Throwable Error 和Exception

    Java异常类层次结构图:       异常的英文单词是exception,字面翻译就是“意外.例外”的意思,也就是非正常情况.事实上,异常本质上是程序上的错误,包括程序逻辑错误和系统错误. 比如使用 ...

  3. Java自学-异常处理 Exception

    Java 异常 Exception 异常定义: 导致程序的正常流程被中断的事件,叫做异常 步骤 1 : 文件不存在异常 比如要打开d盘的LOL.exe文件,这个文件是有可能不存在的 Java中通过 n ...

  4. Java异常处理总结Exception\Error

    Java异常处理总结Exception\Error 2012-12-28 08:17:17|  分类: JAVA |  标签:java  |举报|字号 订阅   Java异常处理总结          ...

  5. 札记:Java异常处理

    异常概述 程序在运行中总会面临一些"意外"情况,良好的代码需要对它们进行预防和处理.大致来说,这些意外情况分三类: 交互输入 用户以非预期的方式使用程序,比如非法输入,不正当的操作 ...

  6. java异常处理(父子异常的处理)

    我当初学java异常处理的时候,对于父子异常的处理,我记得几句话“子类方法只能抛出父类方法所抛出的异常或者是其子异常,子类构造器必须要抛出父类构造器的异常或者其父异常”.那个时候还不知道子类方法为什么 ...

  7. Java 异常处理

    异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的. 比如说,你的代码少了一个分号,那么运行出来结果是提示是错误java.lang.Error:如果你用System.out ...

  8. 《转载》Java异常处理的10个最佳实践

    本文转载自 ImportNew - 挖坑的张师傅 异常处理在编写健壮的 Java 应用中扮演着非常重要的角色.异常处理并不是功能性需求,它需要优雅地处理任何错误情况,比如资源不可用.非法的输入.nul ...

  9. Java异常处理机制 try-catch-finally 剖析

    Java拥有着强大的异常处理机制,最近初步学习了下,感觉内容还是挺多的,特此来将自己的理解写出来与大家分享. 一. 在Java代码code中,由于使用Myeclipse IDE,可以自动提醒用户哪里有 ...

随机推荐

  1. NetCore项目的部署

    NetCore项目和以前的AspNet项目在部署上有很大的不同,因为NetCore是跨平台的 NetCore支持Kestrel和IIS两种方式,看代码的11行和14行 using System.IO; ...

  2. 解决PHPExcel长数字串显示为科学计数

    在excel中如果在一个默认的格中输入或复制超长数字字符串,它会显示为科学计算法,例如身份证号码,解决方法是把表格设置文本格式或在输入前加一个单引号. 使用PHPExcel来生成excel,也会遇到同 ...

  3. POJ3480 John 博弈论 anti-nim anti-SG

    http://poj.org/problem?id=3480 anti-nim其实是anti-SG的一种,就像nim是sg的一种一样.(或者说sg是nim推广?) 看名字就是规则和nim相反,取到最后 ...

  4. bzoj 1712: [Usaco2007 China]Summing Sums 加密

    1712: [Usaco2007 China]Summing Sums 加密 Description     那N只可爱的奶牛刚刚学习了有关密码的许多算法,终于,她们创造出了属于奶牛的加密方法.由于她 ...

  5. Problem A: 插入一个数到数列中

    #include<stdio.h> int main() { ]={,,,,,,,,},i,k; scanf("%d",&n); a[]=n; ;i<=; ...

  6. React中的Keys

    前言 当你在React当中渲染列表项的时候,React会尝试存储对应每个单独项的相关信息,如果你的组件包含state状态数据,那么这些状态数据必须被排序. 当你想要更新这些列表项的时候,React必须 ...

  7. GIT使用(自用)

    1.远程分支就是本地分支push到服务器上的时候产生的.比如master就是一个最典型的远程分支(默认). 1 $: git push origin master 除了master之外,我们还可以随便 ...

  8. XCode6 ,iOS之PCH文件配置

    1: 创建PCH文件 NewFile-->Other中的PCH File-->Next-->Create 2:配置PCH文件 项目中的TARGETS-->Build Setti ...

  9. MYSQL复习笔记2-自带工具介绍

    Date: 20140102Auth: Jin 一.mysql 命令行客户端1)base-h host-P port--socket=path,-S path用于连接的套接字文件替换使用IP PORT ...

  10. VUE2.0学习总结

    摘要: 年后公司项目开始上vue2.0,自己对学习进行了总结,希望对大家有帮助! VUE2.0学习 vue介绍 vue是什么? https://vuefe.cn/guide vue也是一个数据驱动框架 ...