try catch 是抛异常吧。。不是错误的机制
一般错误有两种,一种就是错误,例如:内存溢出,一种是异常,异常是不可避免的,异常又分两种,一种编译时异常一种运行时异常,编译时异常需要程序员放在try{}块中,在catch{}中处理,finally{}表示不管这个异常能不能处理,都需要运行的代码,比如关闭连接,关闭流。编译时异常如果程序员不处理则IDE会报错,而运行时异常是指程序运行时的异常,这种异常不能用try{}catch{}捕获,只能程序员自己发现,然后去修改,例如:nullpointerexception。
温馨提示:答案为网友推荐,仅供参考
第1个回答 2012-07-31
你好,java采用以下几种方式来处理异常。
1.try和catch语句
将要处理的代码放入try块中,然后创建相应的catch块的列表。如果生成都异常与catch中提到的相匹配,那么catch条件中的块语句就被执行。try块后可能有许多catch块,每个都处理不同的异常。每个catch中的参数都是Exception的子类。
2.finally语句
finally语句定义一个总是执行的代码,而不考虑异常是否被捕获。
3.throw引起一个异常
调用申明抛出异常,示例代码如下:
public class Test{
// 静态方法,方法中有异常存在
public static void TestA() throws ArrayIndexOutOfBoundsException{
int a[] = {1, 2, 3};
for (int i = 0; i < 4; i++) {
System.out.println(a[i]);
}
}
// 主函数,调用静态方法
public static void main(String args[]){
try {
TestA();
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e);
}
}
}
throw语句强制抛出异常,示例代码如下:
public class Test{
public static void main(String args[]){
try {
TestA();
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e);
throw new ArrayIndexOutOfBoundsException();
}
}
}本回答被提问者和网友采纳
1.try和catch语句
将要处理的代码放入try块中,然后创建相应的catch块的列表。如果生成都异常与catch中提到的相匹配,那么catch条件中的块语句就被执行。try块后可能有许多catch块,每个都处理不同的异常。每个catch中的参数都是Exception的子类。
2.finally语句
finally语句定义一个总是执行的代码,而不考虑异常是否被捕获。
3.throw引起一个异常
调用申明抛出异常,示例代码如下:
public class Test{
// 静态方法,方法中有异常存在
public static void TestA() throws ArrayIndexOutOfBoundsException{
int a[] = {1, 2, 3};
for (int i = 0; i < 4; i++) {
System.out.println(a[i]);
}
}
// 主函数,调用静态方法
public static void main(String args[]){
try {
TestA();
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e);
}
}
}
throw语句强制抛出异常,示例代码如下:
public class Test{
public static void main(String args[]){
try {
TestA();
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e);
throw new ArrayIndexOutOfBoundsException();
}
}
}本回答被提问者和网友采纳
第2个回答 2012-07-31
防错,也就是处理程序运行遇到错误时,接下来你想要它执行什么
第3个回答 2012-07-31
try catch finnal throw - -