欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

[Java][Java中的错误与异常],

来源: javaer 分享于  点击 7341 次 点评:222

[Java][Java中的错误与异常],


目录

  • Java中的错误与异常的大体介绍
      • 几个例子
          • JVM抛出运行时异常
          • 手动抛出运行时异常
          • 自己定义运行时异常(runtime Exception)
          • 自己定义检查异常1(checked Exception)(声明)
          • 自己定义检查异常2(checked Exception)(捕获)

Java中的错误与异常的大体介绍

1.Java中的不正常情况分为Error与Exception,均继承于Trowable类,具备可抛性。
2.Error发生在JVM层面,例如常见的OutOfMemoryError,StackOverflowError等都属于Error,发生Error时,程序无法继续进行,不推荐对Error进行捕获处理。
3.Exception分为checked Exception与runtime Exception。对于checked Exception 来说,Java编译器强制对其进行捕捉(该说法取自《Java程序员面试笔试宝典》第114页)。对于runtime Exception来说,Java编译器不强制对其进行捕捉。如果不进行捕捉,异常类对象会不断被向上抛出,对于多线程来说最终通过Thread.run()方法抛给JVM,该线程退出;如果是单线程,最终会通过main()方法抛给JVM,主程序退出。

Java程序除了内置了一些异常类,还允许程序员自己定义异常类,让我们通过几个小例子来一步步了解下。

几个例子

JVM抛出运行时异常
import java.lang.ArrayIndexOutOfBoundsException;
class ExceptionTest{
	public static void main(String[] args){
		int[] array = new int [3];
		Demo  d = new Demo();
		int result = d.getElement(array, 3);
		System.out.println(result);
	}
}
class Demo{
	public int getElement(int [] array, int index){
		int result = array[index];
		return result;
	}
}
程序结果:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
        at Demo.getElement(ExceptionTest.java:12)
        at ExceptionTest.main(ExceptionTest.java:6)


说明:如图在getElement()方法中,执行array[index]语句的时候相当于JVM执行了一个抛出语句,将ArrayIndexOutOfBoundsException异常类对象抛给了main()方法,而main()方法中也没有进行捕捉处理,最终抛给了JVM
那么我们是不是可以根据index参数来判断是否越界自己进行抛出动作呢?

手动抛出运行时异常
import java.lang.ArrayIndexOutOfBoundsException;
class ExceptionTest1{
	public static void main(String[] args){
		int[] array = new int [3];
		Demo  d = new Demo();
		int result = d.getElement(array, 3);
		System.out.println(result);
	}
}
class Demo{
	public int getElement(int [] array, int index){
		if(index>=array.length || index<0){
			throw new ArrayIndexOutOfBoundsException("这是我们自己抛出的异常哦");
		}
		return array[index];
	}
}
程序结果:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 这是我们自己抛出的异常哦
        at Demo.getElement(ExceptionTest1.java:13)
        at ExceptionTest1.main(ExceptionTest1.java:6)

Java中万物皆是对象,既然抛出的是对象,那么我们是不是可以自己定义异常类,抛出自己的异常对象呢?

自己定义运行时异常(runtime Exception)
import java.lang.ArrayIndexOutOfBoundsException;
import java.lang.Exception;
public class ExceptionTest2{
	public static void main(String[] args){
		int[] array = new int [3];
		Demo  d = new Demo();
		int result = d.getElement(array, 3);
		System.out.println(result);
	}
}
class Demo{
	public int getElement(int [] array, int index){
		if(index>=array.length){
			throw new myException("这是我们自己定义的异常类哦!");
		}
		return array[index];
	}
}
class myException extends Exception{
	myException(String s){
	super(s);
	}
}
程序结果:
ExceptionTest2.java:14: 错误: 未报告的异常错误myException; 必须对其进行捕获或声明以便抛出
                        throw new myException("这是我们自己定义的异常类哦!");
                        ^
1 个错误

我们发现是有错误的,这是因为我们自己定义的异常类Exception为checked Exception,如果我们将其改为runtime Exception则可以运行成功,代码如下:

import java.lang.ArrayIndexOutOfBoundsException;
import java.lang.Exception;
public class ExceptionTest3{
	public static void main(String[] args){
		int[] array = new int [3];
		Demo  d = new Demo();
		int result = d.getElement(array, 3);
		System.out.println(result);
	}
}
class Demo{
	public int getElement(int [] array, int index){
		if(index>=array.length){
			throw new myException("这是我们自己定义的异常类哦");
		}
		return array[index];
	}
}
class myException extends RuntimeException{
	myException(String s){
	super(s);
	}
}
程序结果:
Exception in thread "main" myException: 这是我们自己定义的异常类哦
        at Demo.getElement(ExceptionTest3.java:14)
        at ExceptionTest3.main(ExceptionTest3.java:7)
自己定义检查异常1(checked Exception)(声明)

我们首先选择声明的方式使ExceptionTest2程序中的自己定义的checked Exception能够正常运行

import java.lang.ArrayIndexOutOfBoundsException;
import java.lang.Exception;
public class ExceptionTest4{
	public static void main(String[] args) throws myException{
		int[] array = new int [3];
		Demo  d = new Demo();
		int result = d.getElement(array, 3);
		System.out.println(result);
	}
}
class Demo{
	public int getElement(int [] array, int index) throws myException{
		if(index>=array.length){
			throw new myException("这是我们自己定义的异常类哦!");
		}
		return array[index];
	}
}
class myException extends Exception{
	myException(String s){
	super(s);
	}
}
程序结果:
Exception in thread "main" myException: 这是我们自己定义的异常类哦!
        at Demo.getElement(ExceptionTest4.java:14)
        at ExceptionTest4.main(ExceptionTest4.java:7)

从中我们可以看出,对于checked Exception来说,不一定要向本文开篇所说的一定要求捕获,不断向上抛出也是可以的。
注意:main()方法中对异常也没有进行捕捉,所以也要进行声明!!!
声明其实告诉了方法调用者(调用者是看不到函数内部的)两件事情:
1.该方法可能会抛出异常,你需要对这种情况进行处理,要么继续向上抛出,要么try,catch进行捕捉。
2.调用者需要定义该异常类(该说法待考证)。

自己定义检查异常2(checked Exception)(捕获)
import java.lang.Exception;
public class ExceptionTest5{
	public static void main(String[] args){
		int[] array = new int[3];
		Demo d = new Demo();
		try{
		int result = d.getElement(array, 3);
		System.out.println(result);
		}
		catch(myException e){
			System.out.println(e);
		}
	}
}
class Demo{
	public int getElement(int[] array , int index) throws myException{
		if(index>=array.length)
			throw new myException("这是我自己定义的异常类哦");
		int result = array[index];
		return result;
	}
}
class myException extends Exception{
	myException(String s){
		super(s);
	}
}
程序结果:
myException: 这是我自己定义的异常类哦

注意1:catch参数中的myException类类型的变量e用于接收异常类的对象,以便在代码块中进行操作

注意2:System.out.println(e);一般输出的是对象e的地址哈希值,但是对于异常类来说JVM做了特殊处理,相当于输出了System.out.println(e.toString());

相关文章

    暂无相关文章
相关栏目:

用户点评