java异常处理

</pre><span style="color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;"></span><pre name="code" class="java"><strong>概念(一般都是参照老师的,代码中多少会有自己的注释):</strong>
一、程序的错误可分三类:1.编译错误   2.逻辑错误   3.运行时错误。
<pre name="code" class="java">

1)编译错误是由于没有遵循Java语言的语法规则而产生的,这种错误要在编译阶段排除,否则程序不可能运行。

2)逻辑错误是指程序编译正常,也能运行,但结果不是人们所期待的。

3)运行时错误是指程序运行过程中出现了一个不可能执行的操作,就会出现运行时错误,运行时错误有时也可以由逻辑错误引起。

异常处理的主要目的是即使在程序运行时发生了错误,也要保证程序能正常结束,避免由于错误而使正在运行的程序中途停止。

<span style="font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体;">异常处理机制是JAVA语言的重要特征之一。通过异常处理机制可防止程序执行期间因出现错误而造成不可预料的结果。</span>
<span style="font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体;"><span style="color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;">程序执行期间可能会发生各种异常:如:</span>
</span>
<span style="font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体;"><span style="color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;"><span style="color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;">① 用户输入出错</span><br style="box-sizing: border-box; color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;" /><span style="color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;"><span style="white-space:pre">	</span>② 所需文件找不到</span><br style="box-sizing: border-box; color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;" /><span style="color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;"><span style="white-space:pre">	</span>③ 运行时磁盘空间不够</span><br style="box-sizing: border-box; color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;" /><span style="color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;"><span style="white-space:pre">	</span>④ 内存不够</span><br style="box-sizing: border-box; color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;" /><span style="color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;"><span style="white-space:pre">	</span>⑤ 算术运算错 (数的溢出,被零除…)</span><br style="box-sizing: border-box; color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;" /><span style="color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;"><span style="white-space:pre">	</span>⑥ 数组下标越界</span>
</span></span>
<span style="font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体;"><span style="color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;"><span style="color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;">二、java程序运行过程中,异常通常分为如下三类:</span></span></span>
<span style="font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体;"><span style="color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;"><span style="color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;">1.<span style="color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;">是JAVA虚拟机由于某些内部错误Error产生的异常,这类异常不在用户程序的控制之内,也不需要用户处理这类异常;</span></span></span></span>
<span style="font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体;"><span style="color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;"><span style="color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;"><span style="color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;">2.<span style="color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;">是标准异常类,由java系统预先定义好的。这类异常是由程序代码中的错误而产生的,如:以零为除数的除法,访问数组下标范围以外的数组元素,</span></span></span></span></span>
<span style="font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体;"><span style="color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;"><span style="color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;"><span style="color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;"><span style="color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;">   访问空对象内的信息,这是需要用户程序处理的异常。</span></span></span></span></span>
<span style="font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体;"><span style="color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;"><span style="color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;"><span style="color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;"><span style="color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;">3.<span style="color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;">.是根据需要在用户程序中自定义的一些异常类。</span></span></span></span></span></span>
<p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; text-indent: 2em; font-size: 14px; color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; line-height: 21px;">三、异常的处理机制</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; text-indent: 2em; font-size: 14px; color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; line-height: 21px;"><span style="text-indent: 2em;">1.抛出(throw)异常</span></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; text-indent: 2em; font-size: 14px; color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; line-height: 21px;">2.捕捉(catch)异常<br style="box-sizing: border-box;" /><span style="white-space:pre">	</span>如果程序在运行过程中出现了运行错误,并且产生的异常与系统中预定义某个异常类相对应,系统就自动产生一个该异常类的对象,</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; text-indent: 2em; font-size: 14px; color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; line-height: 21px;">这个过程称为抛出(throw)异常。当有异常对象抛出时,将在程序中寻找处理这个异常的代码,如果找到处理代码,</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; text-indent: 2em; font-size: 14px; color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; line-height: 21px;">则把异常对象交给该段代码进行处理,这个过程称为捕捉(catch)异常。如果程序中没有给出处理异常的代码,</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; text-indent: 2em; font-size: 14px; color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; line-height: 21px;">则把异常交给Java运行系统默认的异常处理代码进行处理。默认的处理方式是:首先显示描述异常信息的字符串,然后终止程序的运行。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; text-indent: 2em; font-size: 14px; color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; line-height: 21px;"></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; text-indent: 2em; font-size: 14px; color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; line-height: 21px;">四、异常的抛出(throw)</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; text-indent: 2em; font-size: 14px; color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; line-height: 21px;"><span style="white-space:pre">	</span>抛出异常有两种方式:由系统自动抛出和利用抛出语句抛出。<br style="box-sizing: border-box;" /><span style="white-space:pre">	</span>1 由系统自动抛出异常:<br style="box-sizing: border-box;" /><span style="white-space:pre">	</span>在程序运行过程中,如果出现了可被Java运行系统识别的错误,系统会自动产生与该错误相对应的异常类的对象,即自动抛出。<br style="box-sizing: border-box;" /><span style="white-space:pre">	</span>2 人为异常抛出:</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; text-indent: 2em; font-size: 14px; color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; line-height: 21px;">    <span style="color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;">① 在方法头写出需要抛出的异常(利用throws语句);</span></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; text-indent: 2em; font-size: 14px; color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; line-height: 21px;"><span style="white-space:pre"><span style="color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;">    ② 在方法体内抛出异常(利用throw语句)。</span></span></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; text-indent: 2em; font-size: 14px; color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; line-height: 21px;"><span style="white-space:pre"><span style="color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;">五、捕获异常:try catch 语句</span></span></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; text-indent: 2em; font-size: 14px; color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; line-height: 21px;"><span style="white-space:pre"><span style="color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px;"><span style="white-space:pre">	</span></span></span><pre class="brush: java; title: ; notranslate" title="" style="box-sizing: border-box; padding: 9.5px; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(51, 51, 51); border-radius: 4px; margin-top: 0px; margin-bottom: 10px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); background-color: rgb(245, 245, 245);">try  
 {
   //在此区域内或能发生异常; 
 }catch(异常类1   e1)
  { //处理异常1; }
   …
  catch(异常类n   en)
 { //处理异常n; }
 [finally
   { //不论异常是否发生,都要执行的部分; }
 ]

六、用户自定义的异常类

Java提供了很多异常类,但有时还是可以根据需要定义自己的异常类,即创建自定义异常类。
注:
(1)用户自定义的异常类必须是Throwable类或Exception类的子类。
(2)自定义的异常类,一般只要声明两个构造方法,一个是不用参数的,另一个以字符串为参数。

作为构造方法参数的字符串应当反映异常的信息。

(3)用户定义的异常同样要用try–catch捕获,但必须由用户自己抛出 throw new MyException().

<pre name="code" class="java">package cn.exception.day09;
/**
 * 1.抛异常 1.程序运行出错 2.出错产生的异常与系统中预定义某个异常类相对应 3.系统自动产生一个该异常类的对象
 * 
 * 2.捕获异常 1.当有异常对象抛出时,将在程序中寻找处理这个异常的代码, 2.如果找到处理代码,则把异常对象交给该段代码进行处理,
 * 这个过程称为捕捉(catch)异常
 * 
 * try{ //可能发生异常代码块 }catch(ArrayIndexOutOfBoundsException e){ //处理异常代码 }finally{
 * //一定执行,不管有无异常发生都会执行 }
 * 
 *
 */
public class ExceptionTest {

	public static void main(String[] args) {
		/**
		 * 数组下标越界,控制台如下:
		 * 1
			Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
			at cn.exception.day09.ExceptionTest.main(ExceptionTest.java:10)
		 */
		// int[] array=new int[2];
		// for(int i=1;i<3;i++){
		// array[i]=i;
		// System.out.println(array[i]);
		// }
		
		/**
		 * 空指针异常,控制台如下:
			Exception in thread "main" java.lang.NullPointerException
			at cn.exception.day09.ExceptionTest.main(ExceptionTest.java:19)
		 */
		
//			String a=null;
//			int length = a.length();
//			System.out.println(length);
		
		/**异常捕获方法如下:
		 * 如果下面int[] arrays = new int[2];控制台只会报:	1
														数组下标越界2
			不会报下面String类型的空指针异常(如果代码中出现多处异常,只会报第一个异常)。
			
			如果下面int[] arrays = new int[3];控制台则会报:	1
														2
														空指针异常null										
		 */
		try {
			int[] arrays = new int[3];
			for (int j = 1; j < 3; j++) {
				arrays[j] = j;
				System.out.println(arrays[j]);
			}
			System.out.println("1.检测此处会不会输出\n");
			
			String a=null;
			int length = a.length();
			System.out.println(length);
			
			System.out.println("2.检测此处会不会输出\n");//检测结果:此行代码不会输出
			
		} catch (ArrayIndexOutOfBoundsException a) {
			System.out.println("数组下标越界" + a.getMessage());
		}catch(NullPointerException b){
			String message = b.getMessage();
			System.out.println("空指针异常"+message+"\n");
		}finally{
		System.out.println("3.检测此处会不会输出");
		}
	}
}


下面的代码用于判断输入格式,如学生管理系统中学号的判断。

package cn.exception.day09;

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

public class ExceptionDemo {
		
	public static void main(String[] args) {
		inputIntFromKeyBoard();
	}
		public static int inputIntFromKeyBoard(){
			int code = 0;
			boolean a = true;
			System.out.println("请输入命令:");
			while(a){
				try{
					Scanner scanner = new Scanner(System.in);
					code = scanner.nextInt();
					a=false;
				}catch(InputMismatchException e){
					System.out.println("输入命令格式出错!\n重新输入 :");
					a=true;
				}
			}
			return code;
	}	
}

package cn.exception.day09;

public class ThrowTest {

	public static void main(String[] args) throws NegativeException {
		ThrowTest t = new ThrowTest();
//		t.throw1();
//		t.throw2();
		
		/**
		 * 捕获异常
		 */
//		try{
//			t.throw3();
//		}catch(ArithmeticException e){
//			String message = e.getMessage();
//			System.out.println("ArithmeticException:"+message);
//		}
		//t.throw4(2, -20);
		try{
			t.throw4(10, -5);
		}catch(NegativeException d){
			String mess = d.getMessage();
			System.out.println("NegativeException:"+mess);
		}
	}
	/**系统抛出异常mian方法 除数不能为零    控制台如下
	 * Exception in thread "main" java.lang.ArithmeticException: / by zero
		at cn.exception.day09.ThrowTest.throwT(ThrowTest.java:14)
		at cn.exception.day09.ThrowTest.main(ThrowTest.java:7)
	 */
	public void throw1(){
		int a = 15;
		int b = 0;
		
		int c = a/b;
		System.out.println("c:"+c);
	}
	
	/**人为抛出异常方法:控制台如下
	 * Exception in thread "main" java.lang.ArithmeticException: 除数不能为零
		at cn.exception.day09.ThrowTest.throw2(ThrowTest.java:28)
		at cn.exception.day09.ThrowTest.main(ThrowTest.java:8)
	 */
	public void throw2(){
		int a = 15;
		int b = 0;
		if(b==0){
			throw new ArithmeticException("除数不能为零");//用if判断人为抛出异常
		}
		int c = a/b;
		System.out.println("c:"+c);
	}
	
	/**在调用throw3方法时捕获异常:控制台如下:
	 * ArithmeticException:除数不能为零
	 */
	public void throw3(){
		int a = 15;
		int b = 0;
		if(b==0){
			throw new ArithmeticException("除数不能为零");//用if判断人为抛出异常
		}
		int c = a/b;
		System.out.println("c:"+c);
	}
	
	/**上面1 2 3的异常是系统定义的异常类型,现在自定义异常。
	 * 
	 * 1.没有采用捕获控制台如下(与上面一样,只是上面地址为java.lang包;此方法地址为自己的cn.exception.day09):
	 * 		Exception in thread "main" cn.exception.day09.NegativeException: 不能为负数
			at cn.exception.day09.ThrowTest.throw4(ThrowTest.java:65)
			at cn.exception.day09.ThrowTest.main(ThrowTest.java:19)
	 * 2.捕获控制台输出如下:
	 * 		NegativeException:a或者b不能为负数
	 */		
	public void throw4(int a,int b) throws NegativeException{//自定义异常只能人为抛出,使用时需在方法头部显示抛出该异常
<span style="white-space:pre">									</span>如:throws NegativeException
		if(a<0 || b<0){
			throw new NegativeException();
		}
		int c = a+b;
		System.out.println("c:"+c);
	}
}


package cn.exception.day09;
/**
 * 自己创建一个异常类:采用继承
 */
public class NegativeException extends Exception{ //<span style="color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 28px; white-space: pre; background-color: rgb(240, 240, 240);">用户自定义的异常类必须是Throwable类或Exception类的子类。</span>
	public NegativeException(){ //自定义的异常类,一般只要声明两个构造方法,一个是不用参数的,另一个以字符串为参数。</span><p style="margin-top: 0px; margin-bottom: 10px; color: rgb(90, 90, 90); font-family: 'Microsoft YaHei', Arial, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 21px; text-indent: 2em; white-space: pre; box-sizing: border-box; background-color: rgb(240, 240, 240);"><span>	</span>作为构造方法参数的字符串应当反映异常的信息。</p>		super("a或者b不能为负数");
	}
	public NegativeException(String m){
		super(m);
	}
}


package cn.exception.day09;
/**
 * 自己创建一个异常类:采用继承
 * @author shixb
 *
 */
public class NegativeException extends Exception{//用户自定义的异常类必须是Throwable类或Exception类的子类。
	/**
	 * 自定义的异常类,一般只要声明两个构造方法,一个是不用参数的,另一个以字符串为参数。作为构造方法参数的字符串应当反映异常的信息。
	 */
	public NegativeException(){
		super("a或者b不能为负数");	
	}
	public NegativeException(String m){
		super(m);
	}
}







                
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值