标签:java异常 java异常处理 java抛出异常 异常处理 抛出异常
当我们程序遇到异常时,程序会被终止。
如果我们不希望程序被终止怎么办,很简单,可以把有可能发生异常的代码块放进try语句里面,然后在catch语句里面捕获可能发生的异常类型。
try-catch语句:要捕获所有可能发生的异常类型,同一种类型的异常只用一个catch就可以了。
try{
//可能发生异常的代码块
}
catch(Exception1 e){
捕获Exception1类型的异常
}
catch(Exception2 e){
捕获Exception2类型的异常
}
catch(Exception3 e){
捕获Exception3类型的异常
}
//etc...try{
//可能发生异常的代码块
}
finally{
//不管try中代码块中是否产生异常,finally中的代码总是被执行
}
用throws声明抛出异常时,表示将异常交给上一级调用者处理。throws抛出的是异常类,放在方法签名的后面。
void play(){
try {
pain();
} catch (Throwable e) {
e.printStackTrace();//打印异常跟踪信息
}
}
void pain()throws Throwable{
//产生异常代码
}public class MyException {
public static void main(String[] args) throws Throwable{
String str="hello";
str.charAt(10);
}
}我们清楚地看到程序报错了。这是因为此时main()方法没有上级调用者,产生的异常会交给Java编译器或虚拟机处理。非运行时异常编译器处理,不让程序编译通过。运行时异常Java虚拟机处理,JVM对异常的对异常的处理方法是,打印异常的跟踪栈信息,并终止程序的运行。
PS:如果上一级调用者,没有处理被调用抛出的异常时,程序也会被终止。
public class MyException {
public static void main(String[] args) {
throw new StringIndexOutOfBoundsException();
}
}下面我们来看看这个程序。
public class MyException {
public void play() throws Throwable{
try{
String str="lavor_zl";
str.charAt(10);
pain();
}catch(StringIndexOutOfBoundsException e){
throw new StringIndexOutOfBoundsException();
}catch(ArrayIndexOutOfBoundsException e){
throw new ArrayIndexOutOfBoundsException();
}
}
public void pain() throws ArrayIndexOutOfBoundsException{
try{
int[] array=new int[10];
array[12]=2014;
}catch(ArrayIndexOutOfBoundsException e){
throw new ArrayIndexOutOfBoundsException();
}
}
public static void main(String[] args) {
MyException my=new MyException();
try {
my.play();
} catch (Throwable e) {
e.printStackTrace();
}
}
}
上面那个程序弄清楚了后,这个程序也会很容易明白,是一个典型的异常丢失的情况。
public class MyException {
public void play() throws Throwable{
try{
pain();
}catch(ArrayIndexOutOfBoundsException e){
StringIndexOutOfBoundsException c=new StringIndexOutOfBoundsException();
throw c;
}
}
public void pain() throws ArrayIndexOutOfBoundsException{
try{
int[] array=new int[10];
array[12]=2014;
}catch(ArrayIndexOutOfBoundsException e){
throw new ArrayIndexOutOfBoundsException();
}
}
public static void main(String[] args) {
MyException my=new MyException();
try {
my.play();
} catch (Throwable e) {
e.printStackTrace();
}
}
}
我们来看看下面这个程序吧,这个程序用到了异常链。
public class MyException {
public void play() throws Throwable{
try{
pain();
}catch(ArrayIndexOutOfBoundsException e){
StringIndexOutOfBoundsException c=new StringIndexOutOfBoundsException();
c.initCause(e);//异常链
throw c;
}
}
public void pain() throws ArrayIndexOutOfBoundsException{
try{
int[] array=new int[10];
array[12]=2014;
}catch(ArrayIndexOutOfBoundsException e){
throw new ArrayIndexOutOfBoundsException();
}
}
public static void main(String[] args) {
MyException my=new MyException();
try {
my.play();
} catch (Throwable e) {
e.printStackTrace();
}
}
}Throwable类是所有异常的父类。
Throwable有两个子类,ERROR(错误)和Exception。
Exception下面有非运行时异常(编译异常)和运行时异常(RuntimeException)。
非运行时异常需要捕获,不然编译器不让通过。运行时异常可以选择捕获处理,也可以不处理。这些异常一般是由程序逻辑错误引起的,程序应该从逻辑角度尽可能避免这类异常的发生。
我们自定义普通异常时应该继承Exception,定义运行时异常要继承RuntimeException。
/*
自定义异常举例
*/
class MyException extends Exception{
public MyException() {
super();
}
}标签:java异常 java异常处理 java抛出异常 异常处理 抛出异常
原文地址:http://blog.csdn.net/lavor_zl/article/details/40956931