【2014/10/12 21:40】文章待续~

1.函数自身捕获处理异常的情况

以下的样例介绍了try~catch语句中出现异常时语句的运行顺序:

package month10;

import java.lang.*;

public class TryCatch{

	/*
* 函数产生一个ArithmeticException异常
*/
public static void First(){
System.out.println("第一个异常处理的样例");
try{
//double m=5.0/0; 在java中,浮点数double和float能够除0。返回无穷大
int m=5/0;
System.out.println(m+"第一个函数不会继续执行了");
}
catch(ArithmeticException e){
System.out.println("第一个函数捕获了异常");
}
finally{
System.out.println("第一个异常处理的样例结束");
}
} /*
* 函数产生一个OutIndexOfException异常
*/
public static void Second(){
System.out.println("第二个异常处理的样例");
int[] arr=new int[3];
try{
for(int i=0;i<4;i++){
arr[i]=i;
}
}
catch(Exception e){
System.out.println("第二个函数捕获了异常");
}
finally{
System.out.println("第一个异常处理的样例结束");
}
} public static void main(String[] args) { System.out.println("程序执行開始");
First();
Second();
System.out.println("程序执行结束"); } }

运行的结果例如以下:

2. 函数自身不处理异常,抛出异常,直接外层函数处理

try抛出了异常。try语句块中抛出异常之后的语句不会再被运行。

package month10;

import java.lang.*;

public class TryCatch{

	/*
* 函数产生一个ArithmeticException异常
*/
public void First(){
System.out.println("第一个异常处理的样例");
try{
//double m=5.0/0; 在java中,浮点数double和float能够除0,返回无穷大
int m=5/0;
System.out.println(m+"第一个函数不会继续执行了");
}
catch(ArithmeticException e){
//throw用在程序中明白表示这里抛出一个异常。 后面跟一个异常对象(实例).
throw new ArithmeticException("ArithmeticException");
//后面不跟语句
}
finally{
System.out.println("第一个异常处理的样例结束");
}
} /*
* 函数产生一个OutIndexOfException异常
*/
public void Second(){
System.out.println("第二个异常处理的样例");
int[] arr=new int[3];
try{
for(int i=0;i<4;i++){
arr[i]=i;
}
}
catch(ArrayIndexOutOfBoundsException e){
throw new ArithmeticException("ArrayIndexOutOfBoundsException");
}
finally{
//上面的样例这里写的输出语句有错误
System.out.println("第二个异常处理的样例结束");
}
} /*
* 封装了First、Second
*/
public void Thrid(){
System.out.println("函数三将First/Second開始执行");
try{
First();
Second();
}
catch(Exception e){
System.out.println("接受异常"+e.getMessage());
}
System.out.println("函数三之后的语句");
} public static void main(String[] args) { System.out.println("程序执行開始");
TryCatch temp=new TryCatch();
temp.Thrid();
System.out.println("程序执行结束"); } }

函数的运行效果例如以下:

3.函数抛出异常,向上抛,外层函数处理

验证函数执行产生异常,外层函数既没有对异常进行捕获处理、亦没有声明抛出异常、异常信息是否会一直向上抛,直到被处理。

package month10;

import java.lang.*;

public class TryCatch{

	/*
* 函数产生一个ArithmeticException异常
*/
public void First(){
System.out.println("第一个异常处理的样例");
try{
//double m=5.0/0; 在java中,浮点数double和float能够除0,返回无穷大
int m=5/0;
System.out.println(m+"第一个函数不会继续执行了");
}
catch(ArithmeticException e){
//throw用在程序中明白表示这里抛出一个异常。 后面跟一个异常对象(实例).
throw new ArithmeticException("ArithmeticException");
//后面不跟语句
}
finally{
System.out.println("第一个异常处理的样例结束");
}
} /*
* 函数产生一个OutIndexOfException异常
*/
public void Second(){
System.out.println("第二个异常处理的样例");
int[] arr=new int[3];
try{
for(int i=0;i<4;i++){
arr[i]=i;
}
}
catch(ArrayIndexOutOfBoundsException e){
throw new ArithmeticException("ArrayIndexOutOfBoundsException");
}
finally{
//上面的样例这里写的输出语句有错误
System.out.println("第二个异常处理的样例结束");
}
} /*
* 封装了First、Second,
* 函数对First、Second抛出的异常不进行处理
*/
public void Thrid(){ System.out.println("函数三将First/Second開始执行");
First();
Second();
System.out.println("函数三之后的语句");
} /*
* 调用函数Thrid
*/
public void Four(){
try{
Thrid();
}
catch(Exception e){
System.out.println("函数3没有进行异常抛出,可是函数1的异常会一直向上抛,直到被捕获");
}
System.out.println("函数4之后的语句");
} public static void main(String[] args) { System.out.println("程序执行開始");
TryCatch temp=new TryCatch();
temp.Four();
System.out.println("程序执行结束"); } }

执行结果例如以下:

4.验证Checked Exception 是否也符合

上面的样例都验证的是执行时异常。不能全然说明问题。如今,验证Checked Exception。

:函数产生异常,若由函数调用者来捕获处理异常,继续之后的程序代码运行;若产生异常未捕获,异常会沿着调用栈下移,一直找到与之匹配的处理方法,若被处理。从处理的地方開始运行之后的代码;若到达调用栈底仍未找到,程序终止。

加入了CExce()函数,用于生产FileNotFoundException异常。

大家差分吧。

package month10;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.*; public class TryCatch{ /*
* 函数产生一个ArithmeticException异常
*/
public void First(){
System.out.println("第一个异常处理的样例");
try{
//double m=5.0/0; 在java中,浮点数double和float能够除0。返回无穷大
int m=5/0;
System.out.println(m+"第一个函数不会继续执行了");
}
catch(ArithmeticException e){
//throw用在程序中明白表示这里抛出一个异常。 后面跟一个异常对象(实例).
throw new ArithmeticException("ArithmeticException");
//后面不跟语句
}
finally{
System.out.println("第一个异常处理的样例结束");
}
} /*
* 函数产生一个OutIndexOfException异常
*/
public void Second(){
System.out.println("第二个异常处理的样例");
int[] arr=new int[3];
try{
for(int i=0;i<4;i++){
arr[i]=i;
}
}
catch(ArrayIndexOutOfBoundsException e){
throw new ArithmeticException("ArrayIndexOutOfBoundsException");
}
finally{
//上面的样例这里写的输出语句有错误
System.out.println("第二个异常处理的样例结束");
}
} /*
* 封装了First、Second,
* 函数对First、Second抛出的异常不进行处理
* 加入了CExce()函数
*/
public void Thrid(){ System.out.println("函数三将CExce/First/Second開始执行"); CExce();
First();
Second();
System.out.println("函数三之后的语句");
} /*
* 调用函数Thrid
*/
public void Four(){
try{
Thrid();
}
catch(Exception e){
System.out.println("函数3没有进行异常抛出。此时Checked Exception异常函数Cexce的异常自己捕获处理了异常。此时捕获First的异常");
}
System.out.println("函数4之后的语句");
} /*
*
*/
public void CExce(){
FileInputStream fis=null;
FileOutputStream fos=null;
try{
//目录下没有这个文件,会产生FileNoFoundException。 fis=new FileInputStream("C:\\Users\\acer\\Pictures\\boke\\neojos.jpg");
fos=new FileOutputStream("rt.png");
byte[] b=new byte[1024];
int count=0;
while((count=fis.read(b))!=-1){ //int read(byte[] b)
fos.write(b,0,count); //write(byte[]b,int off,int len)
}
}
catch(IOException e){
System.out.println("IO异常咯");
}
finally{
if(fis!=null){
try{
fis.close();
}
catch(IOException e){
System.out.println("error1");
}
}
if(fos!=null){
try{
fos.close();
}
catch(IOException e){
System.out.println("error2");
}
}
//System.out.println("IO over!");
}
} public static void main(String[] args) { System.out.println("程序执行開始");
TryCatch temp=new TryCatch();
temp.Four();
System.out.println("程序执行结束"); }
}

函数执行截图例如以下:

5.函数体声明抛出异常Throws

函数体声明异常,也就是在方法名后面加上throws ExceptionName,..., 方法本身仅仅是抛出异常,由函数调用者来捕获异常。若产生异常,异常会沿着调用栈下移。一直找到与之匹配的处理方法,若到达调用栈底仍未找到。程序终止。

编写代码,你会发现。

对于Throw unchecked Exception的函数,必须在函数体方法声明时追加throws xxException,否则通只是编译器。

在上面的样例中分别在CExce()和Third函数中追加。

import java.lang.*;

public class TryCatch{

	/*
* 函数产生一个ArithmeticException异常
*/
public void First(){
System.out.println("第一个异常处理的样例");
try{
//double m=5.0/0; 在java中。浮点数double和float能够除0。返回无穷大
int m=5/0;
System.out.println(m+"第一个函数不会继续执行了");
}
catch(ArithmeticException e){
//throw用在程序中明白表示这里抛出一个异常。 后面跟一个异常对象(实例).
throw new ArithmeticException("ArithmeticException");
//后面不跟语句
}
finally{
System.out.println("第一个异常处理的样例结束");
}
} /*
* 函数产生一个OutIndexOfException异常
*/
public void Second(){
System.out.println("第二个异常处理的样例");
int[] arr=new int[3];
try{
for(int i=0;i<4;i++){
arr[i]=i;
}
}
catch(ArrayIndexOutOfBoundsException e){
throw new ArithmeticException("ArrayIndexOutOfBoundsException");
}
finally{
//上面的样例这里写的输出语句有错误
System.out.println("第二个异常处理的样例结束");
}
} /*
* 封装了First、Second,
* 函数对First、Second抛出的异常不进行处理
* 加入了CExce()函数
*/
public void Thrid() throws IOException{ System.out.println("函数三将CExce/First/Second開始执行"); CExce();
First();
Second();
System.out.println("函数三之后的语句");
} /*
* 调用函数Thrid
*/
public void Four(){
try{
Thrid();
}
catch(Exception e){
System.out.println("函数3没有进行异常抛出,此时Checked Exception异常函数Cexce的异常自己捕获处理了异常。 此时捕获First的异常");
}
System.out.println("函数4之后的语句");
} /*
*
*/
public void CExce() throws FileNotFoundException{
FileInputStream fis=null;
FileOutputStream fos=null;
try{
//目录下没有这个文件,会产生FileNoFoundException。 fis=new FileInputStream("C:\\Users\\acer\\Pictures\\boke\\neojos.jpg");
fos=new FileOutputStream("rt.png");
byte[] b=new byte[1024];
int count=0;
while((count=fis.read(b))!=-1){ //int read(byte[] b)
fos.write(b,0,count); //write(byte[]b,int off,int len)
}
}
catch(IOException e){
throw new FileNotFoundException("文件未找到异常");
}
finally{
if(fis!=null){
try{
fis.close();
}
catch(IOException e){
System.out.println("error1");
}
}
if(fos!=null){
try{
fos.close();
}
catch(IOException e){
System.out.println("error2");
}
}
//System.out.println("IO over!");
}
} public static void main(String[] args) { System.out.println("程序执行開始");
TryCatch temp=new TryCatch();
temp.Four();
System.out.println("程序执行结束"); }
}

函数运行结果例如以下:

这里图片的内容仅仅是用于提示运行的流程,输出文字没有意义

try~Catch语句中异常的处理过程的更多相关文章

  1. Java异常处理中finally中的return会覆盖catch语句中的return语句

    Java异常处理中finally中的return会覆盖catch语句中的return语句和throw语句,所以Java不建议在finally中使用return语句 此外 finally中的throw语 ...

  2. 在try...catch语句中执行Response.End()后如何停止执行catch语句中的内容

    在调用Response.End()时,会执行Thread.CurrentThread.Abort()操作. 如果将Response.End()放在try...catch中,catch会捕捉Thread ...

  3. C# 中异常抛出捕获机制--throw / try,catch,finally

    try { messagebox.show("true"); } catch { messagebox.show("false"); } finally { m ...

  4. java中异常抛出后代码还会继续执行吗

    今天遇到一个问题,在下面的代码中,当抛出运行时异常后,后面的代码还会执行吗,是否需要在异常后面加上return语句呢? public void add(int index, E element){ i ...

  5. java中异常抛出后代码是否会继续执行

    为了回答这个问题,我编写了几段代码测试了一下,结果如下:  代码1:throw new Exception("参数越界");   System.out.println(" ...

  6. 浅谈java中异常抛出后代码是否会继续执行

    问题 今天遇到一个问题,在下面的代码中,当抛出运行时异常后,后面的代码还会执行吗,是否需要在异常后面加上return语句呢? public void add(int index, E element) ...

  7. 一个问题:关于finally中return吞掉catch块中抛出的异常

    今天遇到一个感觉很神奇的问题,记录一下问题以及自己分析问题的思路. 预警:不知道怎么看java字节码的朋友可能需要先看一下如何阅读java字节码才能看懂后面的解释. 我有一段程序: public cl ...

  8. [JavaScript]catch(ex)语句中的ex

    try/catch语句是JavaScript语句提供的异常处理机制,一旦try语句块内部的语句抛出异常,在catch语句块即可捕获到Error类型的异常信息.我们知道JavaScript里是没有块作用 ...

  9. Sql语法高级应用之六:如何在Sql语句中如何使用TRY...CATCH

    TRY...CATCH使用范例 BEGIN TRY //逻辑语句块 END TRYBEGIN CATCH //Catch异常处理块 SET @msg = ERROR_MESSAGE(); PRINT ...

随机推荐

  1. 2.技巧: 用 JAXM 发送和接收 SOAP 消息—Java API 使许多手工生成和发送消息方面必需的步骤自动化

    转自:https://www.cnblogs.com/chenying99/archive/2013/05/23/3094128.html 技巧: 用 JAXM 发送和接收 SOAP 消息—Java ...

  2. bind()和trigger()额外数据

    $(function(){ $('input').click(function(e,data1,data2,data3,data4){ alert(data1 + '|' + data2 + '|' ...

  3. BZOJ4025: 二分图(LCT)

    Description 神犇有一个n个节点的图.因为神犇是神犇,所以在T时间内一些边会出现后消失.神犇要求出每一时间段内这个图是否是二分图.这么简单的问题神犇当然会做了,于是他想考考你. Input ...

  4. Docker+Solr

    原文:Docker+Solr docker 内的solr并不是部署在tomcat里,而是自启动的.默认的home是/opt/solr/server/solr # docker search solr ...

  5. 老调重弹:JDBC系列 之 &lt;驱动载入原理全面解析&gt;

    前言 近期在研究Mybatis框架,因为该框架基于JDBC.想要非常好地理解和学习Mybatis,必需要对JDBC有较深入的了解.所以便把JDBC 这个东东翻出来.好好总结一番,作为自己的笔记,也是给 ...

  6. Android中的帧动画与补间动画的使用

    前言 在日常开发中,我们有时候须要一些好看的动画效果,这时能够充分利用Android提供的这几种动画来实现. Android提供了3种类型的动画: 补间动画:补间动画能够应用于View,让你能够定义一 ...

  7. SQL server 2012 安装SQL2012出现报错: 启用 Windows 功能 NetFx3 时出错

    在window server 2012服务器上,安装 SQL Server 2012的过程中,报了一个错误,一个安装失败, 在安装SQL 2012的过程中.出现下面错误:启用 Windows 功能 N ...

  8. poi完美word转html(表格、图片、样式)

    直入正题,需求为页面预览word文档,用的是poi3.8,以下代码支持表格.图片,不支持分页,只支持doc,不支持docx: /** * */ import java.io.BufferedWrite ...

  9. arguments对象、apply()、匿名函数

    在学习arguments对象时,碰到的一段code,不是太好理解.原文地址中文(http://www.jb51.net/article/25048.htm).英文(http://www.sitepoi ...

  10. Vijos 1164 曹冲养猪(中国剩余定理)

    P1164曹冲养猪 Accepted 标签:三国争霸[显示标签] 描写叙述 自从曹冲搞定了大象以后,曹操就開始捉摸让儿子干些事业,于是派他到中原养猪场养猪,但是曹冲满不高兴.于是在工作中马马虎虎,有一 ...