“Java异常处理机制”的版本间的差异

来自CloudWiki
跳转至: 导航搜索
finally关键字
 
(未显示同一用户的1个中间版本)
第54行: 第54行:
 
如果发生的异常包含在 catch 块中,异常会被传递到该 catch 块,这和传递一个参数到方法是一样。  
 
如果发生的异常包含在 catch 块中,异常会被传递到该 catch 块,这和传递一个参数到方法是一样。  
  
实例:输入类型检查
+
====动手实例:输入类型检查====
  
 
  <nowiki>package exception;
 
  <nowiki>package exception;
第77行: 第77行:
 
}catch(InputMismatchException e){  
 
}catch(InputMismatchException e){  
 
System.out.println("输入类型不正确 !");
 
System.out.println("输入类型不正确 !");
 +
                        e.printStackTrace();
 
}
 
}
 
s.close();
 
s.close();
第86行: 第87行:
 
思考:上面这段代码,如果不加try...catch语句块 会出现什么结果?
 
思考:上面这段代码,如果不加try...catch语句块 会出现什么结果?
  
 
+
====动手实例:数组下标异常====
  
 
  <nowiki>
 
  <nowiki>
第101行: 第102行:
 
       }catch(ArrayIndexOutOfBoundsException e){
 
       }catch(ArrayIndexOutOfBoundsException e){
 
        System.out.println("Exception thrown  :" + e);
 
        System.out.println("Exception thrown  :" + e);
      }
+
                e.printStackTrace();
 +
      }
 
       System.out.println("Out of the block");
 
       System.out.println("Out of the block");
 
   }
 
   }
第162行: 第164行:
 
try{  
 
try{  
 
int a[] = new int[2];
 
int a[] = new int[2];
        for(int i=0;i<3;i++) {
+
                for(int i=0;i<3;i++) {
        System.out.println("请输入一个整数 :");
+
            System.out.println("请输入一个整数 :");
        a[i]=s.nextInt();  
+
            a[i]=s.nextInt();  
        System.out.println("Access element  :" + a[i]);
+
            System.out.println("Access element  :" + a[i]);
        if(a[i]%2 ==0) {
+
            if(a[i]%2 ==0) {
 
        System.out.println("输入整数为偶数 !");
 
        System.out.println("输入整数为偶数 !");
 
         
 
         
        }else{
+
            }else{
 
    System.out.println("输入整数为奇数 !");
 
    System.out.println("输入整数为奇数 !");
 
}  
 
}  
        a[i] =a[i]/0;
+
            a[i] =a[i]/0;
        }
+
                }
 
 
 
}catch(InputMismatchException e){  
 
}catch(InputMismatchException e){  
 
System.out.println("输入类型不正确 :" +e);
 
System.out.println("输入类型不正确 :" +e);
 +
                        e.printStackTrace();
 
}catch(ArrayIndexOutOfBoundsException e){
 
}catch(ArrayIndexOutOfBoundsException e){
        System.out.println("数组地址越界  :" + e);
+
              System.out.println("数组地址越界  :" + e);
    }catch(Exception e){
+
                      e.printStackTrace();
        System.out.println("未知错误  :" + e);
+
      }catch(Exception e){
 +
            System.out.println("未知错误  :" + e);
 +
                    e.printStackTrace();
 
    }
 
    }
 
    System.out.println("程序运行结束。");
 
    System.out.println("程序运行结束。");
第192行: 第197行:
  
 
常用的几种异常:
 
常用的几种异常:
 +
 
  <nowiki>所有异常的父类:Exception
 
  <nowiki>所有异常的父类:Exception
  
第246行: 第252行:
 
try{  
 
try{  
 
int a[] = new int[2];
 
int a[] = new int[2];
        for(int i=0;i<3;i++) {
+
                for(int i=0;i<3;i++) {
        System.out.println("请输入一个整数 :");
+
            System.out.println("请输入一个整数 :");
        a[i]=s.nextInt();  
+
            a[i]=s.nextInt();  
        System.out.println("Access element  :" + a[i]);
+
            System.out.println("Access element  :" + a[i]);
        if(a[i]%2 ==0) {
+
            if(a[i]%2 ==0) {
 
        System.out.println("输入整数为偶数 !");
 
        System.out.println("输入整数为偶数 !");
 
         
 
         
        }else{
+
            }else{
 
    System.out.println("输入整数为奇数 !");
 
    System.out.println("输入整数为奇数 !");
 
}  
 
}  
        a[i] =a[i]/0;
+
            a[i] =a[i]/0;
 
        }
 
        }
 
 
第262行: 第268行:
 
System.out.println("输入类型不正确 :" +e);
 
System.out.println("输入类型不正确 :" +e);
 
}catch(ArrayIndexOutOfBoundsException e){
 
}catch(ArrayIndexOutOfBoundsException e){
        System.out.println("数组地址越界  :" + e);
+
                System.out.println("数组地址越界  :" + e);
    }catch(Exception e){
+
        }catch(Exception e){
        System.out.println("未知错误  :" + e);
+
                System.out.println("未知错误  :" + e);
    }finally {
+
      }finally {
    s.close();//善后
+
            s.close();//善后
    System.out.println("程序运行结束。");
+
            System.out.println("程序运行结束。");
    }
+
      }
 
     
 
     
 
}
 
}
第307行: 第313行:
 
}
 
}
 
}</nowiki>
 
}</nowiki>
 +
 +
==关于System.out.println(e)与e.printStackTrace()==
 +
 +
在打印异常的时候,除了System.out.println(e)这个方法,还有e.printStackTrace()
 +
 +
在抛出异常的情况下,有很多方法,System.out.println(e);这个方法打印出异常,并且输出在哪里出现的异常;e.printStackTrace()也是打印出异常,但是它还将显示出更深的调用信息。比如说:
 +
 +
A extends ---> B extends---> C
 +
 +
当在创建A的过程中出现问题了,我们抛出异常。
 +
 +
System.out.println(e),除了标准异常外,只打印at A 然后再向外层层输出。
 +
 +
e.printStackTrace(),除了标准异常外,打印
 +
 +
at C
 +
 +
at B
 +
 +
at A
 +
 +
.......再向外层调查。
  
  

2020年4月2日 (四) 06:36的最新版本

异常的来源

异常发生的原因有很多,通常包含以下几大类:

   用户输入了非法数据。
   要打开的文件不存在。
   网络通信时连接中断,或者JVM内存溢出。

这些异常有的是因为用户错误引起,有的是程序错误引起的,还有其它一些是因为物理错误引起的。

常见的一些异常我先总结在这里:

  • IOException:输入输出异常,这通常意味着你的输入或输出语句有错误
  • ArithmeticExecption:算术异常类,算术计算错误
  • NullPointerException:空指针异常类,你可能引用了一个空的变量或方法
  • ClassCastException:类型强制转换异常,可能类型转换时出错了,
  • SQLException:操作数据库异常,可能数据库操作语句错了,
  • FileNotFoundException:文件未找到异常,文件没有找到
  • NegativeArrayException:数组负下标异常,再检查一下数组下标,出现负数了~
  • ArrayIndexOutOfBoundsException:数组下标越界异常,数组的下标超过了最大或最小值、越界了
  • SecturityException:违背安全原则异常
  • EOFException:文件已结束异常
  • NumberFormatException:字符串转换为数字异常,字符串转换为数字时出错了
  • NoSuchMethodException:方法未找到异常,方法没有找到。

异常的处理

try ...catch语句

异常怎么来处理呢 ?非常简单,可以将可能发生异常的语句放到try-catch语句当中来处理。

使用 try 和 catch 关键字可以捕获异常。try/catch 代码块放在异常可能发生的地方。

try/catch代码块中的代码称为保护代码,使用 try/catch 的语法如下:

try
{
   // 位置1
}catch(ExceptionName e1)
{
   //Catch 块
}

Catch 语句包含要捕获异常类型的声明。当保护代码块中发生一个异常时,try 后面的 catch 块就会被检查。

如果发生的异常包含在 catch 块中,异常会被传递到该 catch 块,这和传递一个参数到方法是一样。

动手实例:输入类型检查

package exception;

import java.util.InputMismatchException;
import java.util.Scanner;

public class InputException {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int input=0;
		Scanner s =new Scanner(System.in); 
		System.out.println("请输入一个整数 :");
		try{ 
			input=s.nextInt(); 
			if(input% 2 == 0){ 
				System.out.println("输入整数为偶数 !");
		    }else{
			     System.out.println("输入整数为奇数 !");
			} 
		}catch(InputMismatchException e){ 
			System.out.println("输入类型不正确 !");
                        e.printStackTrace();
		}
		s.close();
	}

}

思考:上面这段代码,如果不加try...catch语句块 会出现什么结果?

动手实例:数组下标异常

// 文件名 : ExcepTest.java
import java.io.*;
public class ExcepTest{
 
   public static void main(String args[]){
      try{
	         int a[] = new int[2];
	         for(int i=0;i<5;i++) {
	        	 System.out.println("Access element  :" + a[i]);
	         }
      }catch(ArrayIndexOutOfBoundsException e){
	         System.out.println("Exception thrown  :" + e);
                 e.printStackTrace();
      }
      System.out.println("Out of the block");
   }
}

以上代码编译运行输出结果如下:

Access element  :0
Access element  :0
Exception thrown  :java.lang.ArrayIndexOutOfBoundsException: 2
Out of the block


多个catch语句块

一个 try 代码块后面跟随多个 catch 代码块的情况就叫多重捕获。

多重捕获块的语法如下所示:

try{
   // 程序代码
}catch(异常类型1 异常的变量名1){
  // 程序代码
}catch(异常类型2 异常的变量名2){
  // 程序代码
}catch(异常类型2 异常的变量名2){
  // 程序代码
}

上面的代码段包含了 3 个 catch块。

可以在 try 语句后面添加任意数量的 catch 块。

如果保护代码中发生异常,异常被抛给第一个 catch 块。

如果抛出异常的数据类型与 ExceptionType1 匹配,它在这里就会被捕获。

如果不匹配,它会被传递给第二个 catch 块。

如此,直到异常被捕获或者通过所有的 catch 块。

实例

该实例展示了怎么使用多重 try/catch。

package exception;

import java.util.InputMismatchException;
import java.util.Scanner;

public class InputException {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner s =new Scanner(System.in); 
		
		
		try{ 
			int a[] = new int[2];
	                for(int i=0;i<3;i++) {
	        	    System.out.println("请输入一个整数 :");
	        	    a[i]=s.nextInt(); 
	        	    System.out.println("Access element  :" + a[i]);
	        	    if(a[i]%2 ==0) {
	        		 System.out.println("输入整数为偶数 !");
	        		 
	        	     }else{
				     System.out.println("输入整数为奇数 !");
	 			} 
	        	    a[i] =a[i]/0;
	                 }
			
		}catch(InputMismatchException e){ 
			System.out.println("输入类型不正确 :" +e);
                        e.printStackTrace();
		}catch(ArrayIndexOutOfBoundsException e){
	               System.out.println("数组地址越界  :" + e);
                      e.printStackTrace();
	       }catch(Exception e){
	             System.out.println("未知错误  :" + e);
                     e.printStackTrace();
	    }
	    System.out.println("程序运行结束。");
	}

}


在运行程序时,上述的try-catch异常处理语句就会捕捉到这个异常,并把它打印出来。

常用的几种异常:

所有异常的父类:Exception

输入输出异常:IOException
算术异常类:ArithmeticExecption
空指针异常类:NullPointerException
类型强制转换异常:ClassCastException

操作数据库异常:SQLException

文件未找到异常:FileNotFoundException
数组负下标异常:NegativeArrayException
数组下标越界异常:ArrayIndexOutOfBoundsException
违背安全原则异常:SecturityException
文件已结束异常:EOFException
字符串转换为数字异常:NumberFormatException
方法未找到异常:NoSuchMethodException

finally关键字

finally 关键字用来创建在 try 代码块后面执行的代码块。

无论是否发生异常,finally 代码块中的代码总会被执行。

在 finally 代码块中,可以运行清理类型等收尾善后性质的语句。

finally 代码块出现在 catch 代码块最后,语法如下:

try{
  // 程序代码
}catch(异常类型1 异常的变量名1){
  // 程序代码
}catch(异常类型2 异常的变量名2){
  // 程序代码
}finally{
  // 程序代码
}

实例

package exception;

import java.util.InputMismatchException;
import java.util.Scanner;

public class InputException {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner s =new Scanner(System.in); 
		
		
		try{ 
			int a[] = new int[2];
	                for(int i=0;i<3;i++) {
	        	    System.out.println("请输入一个整数 :");
	        	    a[i]=s.nextInt(); 
	        	    System.out.println("Access element  :" + a[i]);
	        	    if(a[i]%2 ==0) {
	        		 System.out.println("输入整数为偶数 !");
	        		 
	        	    }else{
				     System.out.println("输入整数为奇数 !");
	 			} 
	        	    a[i] =a[i]/0;
	         }
			
		}catch(InputMismatchException e){ 
			System.out.println("输入类型不正确 :" +e);
		}catch(ArrayIndexOutOfBoundsException e){
	                System.out.println("数组地址越界  :" + e);
	        }catch(Exception e){
	                 System.out.println("未知错误  :" + e);
	       }finally {
	    	        s.close();//善后
	    	        System.out.println("程序运行结束。");
	       }
	    
	}

}


注意下面事项:

   catch 不能独立于 try 存在。
   在 try/catch 后面添加 finally 块并非强制性要求的。
   try 代码后不能既没 catch 块也没 finally 块。
   try, catch, finally 块之间不能添加任何代码。

throws 语句

try catch是直接处理,处理完成之后程序继续往下执行,throw则是将异常抛给它的上一级处理,程序便不往下执行了。

例子:

public class MainClass {
	
        public static void divide(int i,int j) throws Exception {
                
               if(j==0){ throw new Exception();} 
               int temp = i/j;                        //此处会产生异常
               System.out.println("计算结果:"+temp);
               
        }
	public static void main(String[] args)  {
		 try{
			 divide(5,0);	
        }catch(Exception e){//处理抛出的异常。
             System.out.println("出现异常:"+e);    //打印异常信息
             //e.printStackTrace();            //打印完整的异常信息
        }		
        System.out.println("计算结束");
                 
	}
}

关于System.out.println(e)与e.printStackTrace()

在打印异常的时候,除了System.out.println(e)这个方法,还有e.printStackTrace()

在抛出异常的情况下,有很多方法,System.out.println(e);这个方法打印出异常,并且输出在哪里出现的异常;e.printStackTrace()也是打印出异常,但是它还将显示出更深的调用信息。比如说:

A extends ---> B extends---> C

当在创建A的过程中出现问题了,我们抛出异常。

System.out.println(e),除了标准异常外,只打印at A 然后再向外层层输出。

e.printStackTrace(),除了标准异常外,打印

at C
at B
at A
.......再向外层调查。


参考文档:

[1] http://www.runoob.com/java/java-exceptions.html

[2] https://www.zhihu.com/question/28254987/answer/894034083