您的位置:首页 > 编程语言 > Java开发

[零基础学JAVA]Java SE面向对象部分-20.异常的捕获与处理 推荐

2009-02-11 21:43 1031 查看
异常的概念



指程序中断执行的一种指令流

public class Demo01
{
public static void main(String args[])
{
int i = 0;
int j = 10;
System.out.println("程序代码执行之前~~~");
System.out.println(i/j);
System.out.println("程序代码执行之后~~~");
}
}



现在我们反过来哈,j/i

public class Demo01
{
public static void main(String args[])
{
int i = 0;
int j = 10;
System.out.println("程序代码执行之前~~~");
System.out.println(j/i);
System.out.println("程序代码执行之后~~~");
}
}



异常一旦发生,则在异常发生处之后的所有代码都不再被执行了。
异常的分类



只要一发生了异常,则肯定会抛出一个异常类的实例化对象。
面试问题:RuntimeException与 Exception的区别:
· RuntimeException表示异常可以由JVM进行处理
· Exception:表示用户可以自行处理
· 实际上两个异常并没有太多的区别,用户都是可以处理的。
· Error:不能由用户自行处理的异常。



捕获异常(1)




public class Demo02
{
public static void main(String args[])
{
int i = 0;
int j = 10;
System.out.println("程序代码执行之前~~~");
try
{
//下面跟上可能发生错误的代码段
System.out.println(j/i);
System.out.println("-------------------");
}
catch (ArithmeticException ae)
{
//进行异常处理
System.out.println("运算发生了异常~~~");
}
System.out.println("程序代码执行之后~~~");
}
}



加入异常处理之后,程序可以正常的执行完,也可以正常的捕获错误了,但是在try语句中,在发生异常的地方就进行跳转了,所以异常发生语句之后的代码不会被执行。
如果程序中有多个异常要处理呢?
注意:一个新的操作:将字符串的内容变为数字。

public class Demo03
{
public static void main(String args[])
{
String str = "123";
//将字符串变为数字
int i = Integer.parseInt(str);
System.out.println(i*2);
}
}



在此代码之中,要求字符串的内容必须是由数字组成的。
如果不全是数字,我们来看下效果

public class Demo03
{
public static void main(String args[])
{
String str = "123a";
//将字符串变为数字
int i = Integer.parseInt(str);
System.out.println(i*2);
}
}



要求:
要求可以在JAVA运行程序的后面加入运行时参数,参数为两个数相除的内容。

public class Demo04
{
public static void main(String args[])
{
//从运行时处接收参数
int i = Integer.parseInt(args[0]);
int j = Integer.parseInt(args[1]);
System.out.println("程序代码执行之前~~~");
try
{
//下面跟上可能发生错误的代码段
System.out.println(i/j);
System.out.println("-------------------");
}
catch (ArithmeticException ae)
{
//进行异常处理
System.out.println("运算发生了异常~~~");
}
System.out.println("程序代码执行之后~~~");
}
}



在以上的程序之中可能存在那些问题呢?
· 如果没有输入参数的时候,则会有错误:ArrayIndexOutOfBoundsException(数组下标越界)



· 如果输入的参数的内容不是数字,则会有错误:NumberFormatException(数字格式化异常)



· 如果输入的被除数为零,则会有错误:ArithmeticException(算术异常)



则此程序代码只有一个catch肯定是不够的,需要处理多个异常。

public class Demo05
{
public static void main(String args[])
{
//从运行时处接收参数
int i = 0;
int j = 0;
System.out.println("程序代码执行之前~~~");
try
{
i=Integer.parseInt(args[0]);
j=Integer.parseInt(args[1]);
//下面跟上可能发生错误的代码段
System.out.println(i/j);
System.out.println("-------------------");
}
catch(ArithmeticException ae)
{
//进行异常处理
System.out.println(ae);
System.out.println("1.运算发生了异常~~~");
}
catch(NumberFormatException ne)
{
System.out.println(ne);
System.out.println("2.输入的内容不是数字~~~");
}
catch(ArrayIndexOutOfBoundsException ae)
{
System.out.println(ae);
System.out.println("3.输入的参数个数不正确~~~");
}
System.out.println("程序代码执行之后~~~");
}
}



以上的程序只要是有异常发生了,则会自动找到对应的catch语句,分别进行处理,但是一个程序可能会存在各种问题,那么有可能全部的catch都写出来吗?


ArithmeticException是Exception的子类哈~



public class Demo06
{
public static void main(String args[])
{
//从运行时处接收参数
int i = 0;
int j = 0;
System.out.println("程序代码执行之前~~~");
try
{
i=Integer.parseInt(args[0]);
j=Integer.parseInt(args[1]);
//下面跟上可能发生错误的代码段
System.out.println(i/j);
System.out.println("-------------------");
}
catch(ArithmeticException ae)
{
//进行异常处理
System.out.println(ae);
System.out.println("1.运算发生了异常~~~");
}
catch(NumberFormatException ne)
{
System.out.println(ne);
System.out.println("2.输入的内容不是数字~~~");
}
catch(ArrayIndexOutOfBoundsException ae)
{
System.out.println(ae);
System.out.println("3.输入的参数个数不正确~~~");
}
catch(Exception e)
{
System.out.println("异常处理");
}
System.out.println("程序代码执行之后~~~");
}
}



以上代码需要注意:
在异常处理中,对于catch语句要求处理更粗的异常要放在处理更细的异常之后。

我们验证下效果哈~

public class Demo07
{
public static void main(String args[])
{
//从运行时处接收参数
int i = 0;
int j = 0;
System.out.println("程序代码执行之前~~~");
try
{
i=Integer.parseInt(args[0]);
j=Integer.parseInt(args[1]);
//下面跟上可能发生错误的代码段
System.out.println(i/j);
System.out.println("-------------------");
}
catch(Exception e)
{
System.out.println("异常处理");
}
catch(ArithmeticException ae)
{
//进行异常处理
System.out.println(ae);
System.out.println("1.运算发生了异常~~~");
}
catch(NumberFormatException ne)
{
System.out.println(ne);
System.out.println("2.输入的内容不是数字~~~");
}
catch(ArrayIndexOutOfBoundsException ae)
{
System.out.println(ae);
System.out.println("3.输入的参数个数不正确~~~");
}
System.out.println("程序代码执行之后~~~");
}
}




public class Demo08
{
public static void main(String args[])
{
//从运行时处接收参数
int i = 0;
int j = 0;
System.out.println("程序代码执行之前~~~");
try
{
i=Integer.parseInt(args[0]);
j=Integer.parseInt(args[1]);
//下面跟上可能发生错误的代码段
System.out.println(i/j);
System.out.println("-------------------");
}
//只要是程序运行时产生的异常肯定可以使用Exception进行接收
catch(Exception e)
{
System.out.println("异常处理");
}
System.out.println("程序代码执行之后~~~");
}
}



捕获异常(2)



不管是否发生了异常,都要执行finally代码。







class Math{
public int div(int i,int j){
return i/j;
}
}
public class Demo10{
public static void main(String args[]){
System.out.println(new Math().div(1,1));
}
}
这个代码现在没问题哈~




class Math{
//表示此方法必须被处理异常,必须在调用处处理
public int div(int i,int j) throws Exception{
return i/j;
}
}
public class Demo10{
public static void main(String args[]){
System.out.println(new Math().div(1,1));
}
}



我们必须要对此代码进行catch捕获

class Math{
//表示此方法必须被处理异常,必须在调用处处理
public int div(int i,int j) throws Exception{
return i/j;
}
}
public class Demo10{
public static void main(String args[]){
try{
System.out.println(new Math().div(1,1));
}catch(Exception e){
System.out.println(e);
}
}
}



正常执行输出哈

class Math{
//表示此方法必须被处理异常,必须在调用处处理
public int div(int i,int j) throws Exception{
return i/j;
}
}
public class Demo10{
public static void main(String args[]){
try{
System.out.println(new Math().div(1,0));
}catch(Exception e){
System.out.println(e);
}
}
}
出现异常时成功捕获异常



同样道理,既然可以在方法上声明,则肯定也可以在main方法处声明。

class Math{
//表示此方法必须被处理异常,必须在调用处处理
public int div(int i,int j) throws Exception{
return i/j;
}
}
public class Demo11{
//此处的错误交给JVM进行处理了
public static void main(String args[]) throws Exception{
System.out.println(new Math().div(1,0));
}
}










public class Demo12{
public static void main(String args[]){
throw new Exception("自己抛出的异常~~~");
}
}



提示要进行try...catch的处理哈~

public class Demo12{
public static void main(String args[]){
try{
throw new Exception("自己抛出的异常~~~");
}catch(Exception e){
System.out.println(e);
}
}
}
这就是throw关键字的作用哈~~~



实际上在开发中:finally、throw、throws都是一起联合使用的。
要求:
定义一个除法操作,要求进入方法之前必须有开始进行除法操作的提示,操作完之后要求有除法操作完成的提示。

class Math{
public int div(int i,int j){
System.out.println("开始除法操作~~~");
int temp=0;
temp=i/j;
System.out.println("开始除法操作~~~");
return temp;
}
}
public class Demo13{
public static void main(String args[]){
System.out.println(new Math().div(1,1));
}
}




class Math{
public int div(int i,int j){
System.out.println("开始除法操作~~~");
int temp=0;
temp=i/j;
System.out.println("开始除法操作~~~");
return temp;
}
}
public class Demo13{
public static void main(String args[]){
System.out.println(new Math().div(1,0));
}
}
这个就出现异常了哈,结束语句也就不出来了



我们要将异常交给调用处处理哈~~~

class Math{
//所有的异常肯定交给调用处处理
public int div(int i,int j) throws Exception{
System.out.println("开始除法操作~~~");
int temp=0;
temp=i/j;
System.out.println("结束除法操作~~~");
return temp;
}
}
public class Demo13{
public static void main(String args[]){
try{
System.out.println(new Math().div(1,0));
}catch(Exception e){
System.out.println(e);
}
}
}



class Math{
//所有的异常肯定交给调用处处理
public int div(int i,int j) throws Exception{
System.out.println("开始除法操作~~~");
int temp=0;
//所有的异常不能自己处理,必须交给被调用处处理
try{
temp=i/j;
}catch(Exception e){
//需要把异常往上抛
throw e;
}finally{
System.out.println("结束除法操作~~~");
}
return temp;
}
}
public class Demo13{
public static void main(String args[]){
try{
System.out.println(new Math().div(1,0));
}catch(Exception e){
System.out.println(e);
}
}
}




自定义异常,如果学习SSH框架,很多的异常不是JDK本身提供的,需要自己定义的。 任何一个类只要继承了Exception类就表示是一个异常类了。

class MyException extends Exception{
public MyException(String msg){
super(msg);
}
public String toString(){
return super.getMessage();
}
}
public class Demo14{
public static void main(String args[]){
try{
throw new MyException("自己定义的异常~~~");
}catch(Exception e){
System.out.println(e);
}
}
}



总结
异常的捕获:try{}catch:try捕获异常,之后把异常类的实例化对象与catch中的类型相比较,如果合适,则进行异常的处理。
Finlly关键字
面试题:
请说出:final和finally关键字的区别
throw和throws关键字
##############################################################
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐