设为首页 加入收藏

TOP

Java异常处理机制(一)
2015-07-16 12:55:00 来源: 作者: 【 】 浏览:6
Tags:Java 异常 处理 机制

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
? ? ? ?

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Java的堆(Heap)和栈(Stack)的.. 下一篇Android利用canvas画画板

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: