Java--异常机制

一:异常的概念与原理

1 . 异常是指在程序的运行过程中所发生的不正常的事件,它会中断正在运行的程序 

2 . Java的异常处理是通过5个关键字来实现的: try、catch finally、throw、throws


3 . Java程序编译和运行时所发生的问题有两大类:

错误(Error)JVM系统内部错误或资源耗尽等严重情况-属于JVM需要负担的责任

异常(Exception):其它因编程错误或偶然的外在因素导致的一般性问题。

程序员通常只能处理异常(Exception),而对错误(Error)无能为力。

4 . 异常处理机制的原理

Java程序在执行过程中如果出现异常,会自动生成一个异常类对象,该异常对象将被自动提交给JVM(在程序没有显式处理异常的情况下),这个过程称为抛出(throw)异常。

package throwdemo;
import java.io.IOException;
public class ThrowDemo {
	public static void main(String[] args) {
		try{
			throw new ArithmeticException();
		}catch(Exception e){
			System.out.println("发生算数异常了...");
		}
		int a=100;
		System.out.println("end....");
	}
}

运行结果:


二:异常的分类

1.常见异常:

    RuntimeException

    ArithmeticException :数学计算异常

    NullPointerException :空指针异常

    ArrayOutOfBoundsException:数组索引越界异常

    ClassCastException:类型转换异常

    SQLException

    IOException

    ClassNotFoundException

2.ExceptionRuntimeException这两个类的区别如下:

    Exception 在程序中是必须进行处理

    RuntimeException可以不使用trycatch进行处理,但是如果有异常产生,则异常将由JVM进行处理。

package exception;
import java.io.IOException;
public class CheckException {
	public static void main(String[] args) {
		Runtime r=Runtime.getRuntime();		
		try {
			r.exec("calc");
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
}

运行结果:


package exception;
public class RuntimeExceptionDemo {
	public static void main(String[] args) {
		int a=100;
		int b=0;
		String str=null;
		int[] array=new int[5];
		try{
			array[5]=100;
			str.equals("abc");
			int c=a/b;
		}catch(NullPointerException e){
			System.out.println("空指针异常~~~");
		}catch(ArithmeticException e){
			System.out.println("算数异常~~~");
		}catch(Exception e){
			System.out.println("以上catch都没有捕获到异常,由最大父类Exception处理异常");
		}finally{
			System.out.println("无论异常是否发生,都会执行");
		}
		
		System.out.println("main()方法执行完毕!");

	}
}

运行结果:


三:处理异常的方法

1. try-catch块

使用try-catch块捕获异常,catch块可以有多个

常见的异常类型:

异常类型

说明

Exception

异常层次结构的父类

ArithmeticException

算术错误情形,如以零作除数

ArrayIndexOutOfBoundsException

数组下标越界

NullPointerException

尝试访问

null

对象成员

ClassNotFoundException

不能加载所需的类

IllegalArgumentException

方法接收到非法参数

ClassCastException

对象强制类型转换出错

NumberFormatException

数字格式转换异常,如把"abc"转换成数字

2.try-catch-finally

try-catch块后加入finally块是否发生异常都执行,不执行的唯一情况

package exception;
public class Finally {
	public static void main(String[] args) {
		int result=method();
		System.out.println("结果是:"+result);
	}	
	public static int method(){
		try{
			int a=100;
			int b=1;
			int c=a/b;
			return c;
		}catch(Exception e){
			System.out.println("发生异常啦...");
		}finally{
			return 999;
		}
	}
}

运行结果:

3.多重catch

引发多种类型的异常

排列catch 语句的顺序:先子类后父类

发生异常时按顺序逐个匹配,只执行第一个与异常类型匹配的catch语句

4.tryfinally

tryfinally 不能捕获异常 ,仅仅用来当发生异常时,用来释放资源

5.throw与throws的区别

 throw用来手动抛出异常对象。 

 throws用于方法声明处,用于抛出该方法体内部可能发生的异常类型。一旦在方法声明处通过throws抛出某种类型的异常,则在该方法体内部就不用处理该类型的异常,交给方法调用处处理该类型的异常。 

四:自定义异常

1.创建自定义异常

继承自Exception 。习惯上包装一下父类的构造方法。

public class MyException extends Exception {
    public MyException() {        super();    } 
    public MyException(String msg) {       
            super(msg);    
    } 
}

2.使用自定义异常

public String[] createArray(int length) throws MyException {
    if (length < 0) {    
        throw new MyException("数组长度小于0,不合法");
    } 
    return new String[length]; 
} 

例题:船的最大载重量为1000,模拟船载货物。

package boat;
public class Boat {
private static final int MAX_STORE=1000;   // 船的最大载重量
private int currentStore;    // 当前载货量
   // 添加货物的方法
   public void load(int num)throws LoadException{
	   System.out.println("载货之前的当前载货量:"+currentStore);
	   this.currentStore+=num;  // 模拟装载货物
	   System.out.println("载货之后的当前载货量:"+currentStore);
	   if(this.currentStore>MAX_STORE){
		   throw new LoadException("货物已经超过最大载重量,船被压坏了!");
	   }
   }  
}
package boat;
// 自定义异常类
public class LoadException extends Exception{
   public LoadException(){
	   
   }
   public LoadException(String msg){
	   super(msg);
   }
}
package boat;
import java.util.Scanner;
public class TestBoat {
	public static void main(String[] args) {
		Boat boat=new Boat();
		while(true){
			Scanner scan=new Scanner(System.in);
			System.out.print("请输入要加入的量:");
			int num=scan.nextInt();
			try {
				boat.load(num);
			} catch (LoadException e) {
				System.out.println(e.getMessage());
				break;
			}
			scan.close();
		}
	}
}

运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值