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

java的异常处理

2007-08-03 00:36 429 查看
java的异常处理提供语言级对运行时错误的处理机制。异常处理机制包括异常类体系,异常处理的try 语句。抛出自定义异常对象的throw语句,声明方法抛出异常的throws子句。这些功能使得程序不仅能够捕获并处理异常,还能够主动抛出异常,也能够将异常向调用传递。

java.lang.Object


java.lang.Throwable


java.lang.Exception

java.lang.Object


java.lang.Throwable


java.lang.Exception


java.lang.RuntimeException

RuntimeException
是那些可能在 Java 虚拟机正常运行期间抛出的异常的超类。

可能在执行方法期间抛出但未被捕获的
RuntimeException
的任何子类都无需在
throws
子句中进行声明。

Throwable
类是 Java 语言中所有错误或异常的超类。只有当对象是此类(或其子类之一)的实例时,才能通过 Java 虚拟机或者 Java
throw
语句抛出。类似地,只有此类或其子类之一才可以是
catch
子句中的参数类型。

两个子类的实例,
Error
Exception
,通常用于指示发生了异常情况。通常,这些实例是在异常情况的上下文中新近创建的,因此包含了相关的信息(比如堆栈跟踪数据)。

=============================================================================================

setStackTrace

public void setStackTrace(StackTraceElement[] stackTrace)

设置将由
getStackTrace()
返回,并由
printStackTrace()
和相关方法输出的堆栈跟踪元素。 此方法设计用于 RPC 框架和其他高级系统,允许客户端重写默认堆栈跟踪,这些默认堆栈跟踪要么在构造 throwable 时由
fillInStackTrace()
生成,要么在从序列化流读取 throwable 时反序列化。

参数:
stackTrace
- 要与此
Throwable
关联的堆栈跟踪元素。指定的数组由此调用复制;在方法调用返回后,指定数组中的改变将不会对此
Throwable
的堆栈跟踪产生影响。

printStackTrace

public void printStackTrace(PrintWriter s)

将此 throwable 及其追踪输出到指定的 PrintWriter。

参数:
s
- 用于输出的
PrintWriter

fillInStackTrace

[code]public Throwable fillInStackTrace()

在异常堆栈跟踪中填充。此方法在
Throwable
对象信息中记录有关当前线程堆栈帧的当前状态。

返回: 对此
Throwable
实例的引用。

getStackTrace

public StackTraceElement[] getStackTrace()

提供编程访问由
printStackTrace()
输出的堆栈跟踪信息。返回堆栈跟踪元素的数组,每个元素表示一个堆栈帧。数组的第零个元素(假定数据的长度为非零)表示堆栈顶部,它是序列中最后的方法调用。通常,这是创建和抛出该 throwable 的地方。数组的最后元素(假定数据的长度为非零)表示堆栈底部,它是序列中第一个方法调用。
某些虚拟机在某些情况下可能会从堆栈跟踪中省略一个或多个堆栈帧。在极端情况下,没有该 throwable 堆栈跟踪信息的虚拟机可以从该方法返回一个零长度数组。一般说来,由此方法返回的数组将包含由 printStackTrace 输出的每帧的一个元素。

返回: 堆栈跟踪元素的数组,表示与此 throwable 相关的堆栈跟踪。[/code]
===============================================================================================


运行的异常都是RuntimeException的子类,说明如下:


1。算术异常:ArithmeticException


2。空对象异常:NullPointException


3。类型强制转换异常:ClassCastException


4.负数组长度异常NegativeArraySizeException


5.数组下标越界异常:ArrayIndexOutOfBoundsException


6.数值格式异常:NumberFormatException


一个异常对象经历抛出,捕获及处理过程。创建一个异常类对象的过程称为 抛出(throw)异常,获得异常对象的过程称为捕获(catch)异常;对异常对象执行相应操作的过程称为处理异常,异常对象由捕获它的语句进行处理。通常,这几个过程分别由不同方法或java虚拟机完成。


=======================================================================================


package Exception;


public class ArrayAverage2 {
public static double average(int table[])
{
if(table!=null&&table.length!=0)
{
double sum=0;
for(int i=0;i<table.length;i++)
sum+=table[i];
return sum/table.length;
}
return 0.0;
}
public static double average(int table1[],int table2[])
{
int count=0;
if(table1!=null&&table1.length!=0)
count=table1.length;
if(table2!=null&&table2.length!=0)
count+=table2.length;
if(count!=0)
return (average(table1)*table1.length+average(table2)*table2.length)/count;
else
return 0.0;
}
public static int[] tointArray(String str[])
{
if(str!=null&&str.length!=0)
{
int temp[]=new int[str.length];
int count=0;
for(int i=0;i<str.length;i++)
try
{
temp[count]=Integer.parseInt(str[i]);
count++;
}
catch(NumberFormatException e)
{
System.out.println("字符串"+str[i]+"不能转换为整数,产生的异常类是"+e.getClass().getName());
}
int table[]=new int[count];
System.arraycopy(temp, 0, table, 0, count);   //复制数组
return table;
}
return null;
}
public static void print(int table[])
{
if(table!=null)
{
System.out.print("数组元素为:");
for(int i=0;i<table.length;i++)
System.out.print("    "+table[i]);

System.out.println();
}
}
public static void main(String[] args) {
int x[]={1,2,3,4};
int y[]={};
System.out.println("average(x,y)="+average(x,y));

y=tointArray(args);
print(y);
System.out.println("average(y)="+average(y));
}


}
========================================================================================


package Exception;


public class IllegalAgeException extends Exception{
public IllegalAgeException(String s)
{
super(s);
}
public IllegalAgeException()
{
this("");
}


}
package Exception;


public class Person3 {
protected String name;
protected int age;
public Person3(String name,int age) throws IllegalAgeException
{
this.set(name);
this.set(age);
}
public void set(int age) throws IllegalAgeException
{
if(age>=0&&age<=100)
this.age=age;
else
throw new IllegalAgeException(""+age);
}
public void set(String name)
{
if(name!=null||name!="")
this.name=name;
else
this.name="名字不知道";
}
public void print()
{
System.out.println(this.toString());
}
public static void main(String[] args) {
Person3 p1=null;
try
{
p1=new Person3("李小名",20);
//调用声明抛出异常方法,必须写在 try 语句中,否则编译不通过
p1.set(120);
}
catch(IllegalAgeException e)   //捕获自定义异常类,而非Exception类
{
e.printStackTrace();     //显示异常饯跟踪信息
}
finally
{
p1.print();   // 显示对象信息
}


}


}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: