您的位置:首页 > 编程语言 > Java开发

Java: 异常处理机制

2015-07-11 22:24 681 查看

1. 如何捕获异常

try

{

可能会出现异常的代码段;

}

catch(异常类型名 处理该异常对象)

{

异常处理代码段;

}

import java.io.*;

public class TryCatchTest {

public static void main(String[] args) {
File file = new File("abc.txt");
int a[] = {1, 2};

try
{
System.out.println(3/0);
}
catch(ArithmeticException e1)
{
System.out.println("3/0: ");
System.out.println("This is ArithmeticException");
}

try
{
System.out.println(a[2]);
}
catch(ArrayIndexOutOfBoundsException e2)
{
System.out.println("a[2] is out of Array: ");
System.out.println("This is ArrayIndexOutOfBoundsException");
}

try
{
BufferedReader input = new BufferedReader(new FileReader(file));
}
catch (FileNotFoundException e3)
{
System.out.println("abc.txt is not found: ");
System.out.println("This is FileNotFoundException");
}
catch(IOException e)
{
System.out.println("This is IOException");
}

}

}


3/0:
This is ArithmeticException
a[2] is out of Array:
This is ArrayIndexOutOfBoundsException
abc.txt is not found:
This is FileNotFoundException

2. 如何抛出异常

编写代码过程中,如果不想在这段代码中捕捉和处理一个可能出现的异常,那么就需要将这个异常传递出去,传递给调用它的方法去处理该异常。这个时候就需要使用throw 和throws

throws语句在方法声明中使用,抛出异常

throw语句在方法体内部使用,抛出异常

注意: 方法体中若使用了throw语句抛出异常,则必须在该方法声明中,采用throws语句来声明该方法体中抛出的异常,同时,throws语句声明抛出的异常,必须是方法体中throw语句抛出的异常或该异常的父类。

import java.io.*;

public class ThrowTest {

public void throwTest1() throws ArithmeticException
{
System.out.println(3/0);
}

public void throwTest2() throws ArrayIndexOutOfBoundsException
{
int a[] ={1,2};
System.out.println(a[2]);
}

public void throwTest3() throws FileNotFoundException
{
File file=new File("abc.txt");
new BufferedReader(new FileReader(file));
}

public void throwTest4() throws FileNotFoundException
{
throw new FileNotFoundException("abc.txt");
}

public static void main(String[] args) {
ThrowTest throwTest=new ThrowTest();

try
{
throwTest.throwTest1();
}
catch (ArithmeticException e1)
{
System.out.println("3/0: ");
System.out.println("This is ArithmeticException");
}

try
{
throwTest.throwTest2();
}
catch(ArrayIndexOutOfBoundsException e2)
{
System.out.println("a[2] is out of Array: ");
System.out.println("This is ArrayIndexOutOfBoundsException");
}

try
{
throwTest.throwTest3();
}
catch (FileNotFoundException e3)
{
System.out.println("abc.txt is not found: ");
System.out.println("This is FileNotFoundException");
}

try
{
throwTest.throwTest4();
}
catch (FileNotFoundException e3)
{
System.out.println("abc.txt is not found: ");
System.out.println("This is FileNotFoundException");
}

}

}


3/0:
This is ArithmeticException
a[2] is out of Array:
This is ArrayIndexOutOfBoundsException
abc.txt is not found:
This is FileNotFoundException
abc.txt is not found:
This is FileNotFoundException

3. 自定义异常

建立自己的异常类,要做的只是根据需要,从Exception类或者从Exception类的子类中继承出需要的类。习惯上,会经常为每一个异常类,提供一个默认的和一个包含详细信息的构造器。需要注意的是,自定义异常类,必须由程序员使用throw语句抛出。

public class MyException {

public static void main(String[] args) {
String str="2abcde";

try
{
char c=str.charAt(0);
if(c<'a'||c>'z'||c<'A'||c>'Z')
throw new FirstLetterException();
}
catch (FirstLetterException e)
{
System.out.println("This is FirstLetterException");
}

}

}

class FirstLetterException extends Exception{
public FirstLetterException()
{
super("The first char is not a letter");
}

public FirstLetterException(String str)
{
super(str);
}
}


This is FirstLetterException

public class MyException {

public static void main(String[] args) throws FirstLetterException{
throw new FirstLetterException();
}
}

class FirstLetterException extends Exception{
public FirstLetterException()
{
super("The first char is not a letter");
}

public FirstLetterException(String str)
{
super(str);
}
}


Exception in thread "main" FirstLetterException: The first char is not a letter
at MyException.main(MyException.java:5)

4. 使用finally语句

在使用try...catch语句是,若try语句中的某一句出现异常情况,那么这部分try语句段中,从出现异常的语句开始,之后的所有语句都不会被执行,直到这部分try语句段结束。

但是在很多情况下,希望无论是否出现异常,某些语句都需要被执行。那么就可以把这部分代码放在finally语句段中,即使try或catch语句段中含有return语句,程序都会在异常抛出后先执行finally语句段,除非try或catch语句段中执行System.exit()方法,或者是出现Error错误,finally语句才不会被执行而退出程序。

import java.io.*;

public class FinallyTest {

public static void main(String[] args) {
File file=null;
BufferedReader input=null;
file=new File("abc.txt");

try
{
input=new BufferedReader(new FileReader(file));
}
catch(FileNotFoundException e)
{
System.out.print("abc.txt is not found: ");
System.out.println("This is FileNotFoundException");
}
finally
{
System.out.println("This is finally code part.");
}

}

}


abc.txt is not found: This is FileNotFoundException
This is finally code part.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: