java异常处理

package maxt;

public class asdd {
     
	public static void main(String[] args){
     
		try{
     
			int a=args.length;
			System.out.println("\na = "+ a);
			int b=42/a;
			int c[]={
     1};
			c[42]=99;
		}catch(ArithmeticException e){
     
			System.out.println("发生了被 0除:"+e);
		}catch(ArrayIndexOutOfBoundsException e){
     
			System.out.println("数组下标越界:"+e);
			}finally{
     
				System.out.println("最后执行finally语句");
			}
		}
	}


a = 0
发生了被 0除:java.lang.ArithmeticException: / by zero
最后执行finally语句

package maxt;
public class asdd {
     
	static void throwProcess() {
     
		try {
     
		throw new NullPointerException("空指针异常");
		} catch (NullPointerException e) {
     
		System.out.println("\n 在 throwProcess 方法中捕获一个" +e.getMessage());
		throw e;
		}
	}
		public static void main(String args[]){
     
		try {
     
		throwProcess();
		} catch (NullPointerException e) {
     
		System.out.println("再次捕获:" + e);
		}
		}
}

在 throwProcess 方法中捕获一个空指针异常
再次捕获:java.lang.NullPointerException: 空指针异常

你可能感兴趣的:(java,异常处理)