Java教程 - Java异常语句
为了防止和处理运行时错误,请将代码包含在try块中进行监视。
紧跟在try块之后,包括一个catch子句它指定您希望捕获的异常类型。
Java try catch语句public class Main {
public static void main(String args[]) {
try { // monitor a block of code. int d = 0;
int a = 42 / d;
System.out.println("This will not be printed.");
} catch (ArithmeticException e) { // catch divide-by-zero error System.out.println("Division by zero.");
}
System.out.println("After catch statement.");
}
}
上面的代码生成以下结果。
多个catch子句
您可以指定两个或多个catch子句,每个捕获不同类型的异常。
当抛出异常时,将按顺序检查每个catch语句,并执行类型与异常类型匹配的第一个。
在执行一个catch语句之后,绕过其他catch语句,并在try/catch块之后继续执行。public class Main {
public static void main(String args[]) {
try {
int a = args.length;
System.out.println("a = " + a);
int b = 42 / a;
int c[] = { 1 };
c[42] = 99;
} catch (ArithmeticException e) {
System.out.println("Divide by 0: " + e);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array index oob: " + e);
}
System.out.println("After try/catch blocks.");
}
}
当您使用多个catch语句时,异常子类必须在它们的任何超类之前。
上面的代码生成以下结果。
嵌套try语句
try语句可以嵌套。public class Main {
public static void main(String args[]) {
try {
int a = args.length;
int b = 42 / a;
System.out.println("a = " + a);
try { // nested try block if (a == 1)
a = a / (a - a); // division by zero exception if (a == 2) {
int c[] = { 1 };
c[4] = 9; // an out-of-bounds exception }
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array index out-of-bounds: " + e);
}
} catch (ArithmeticException e) {
System.out.println("Divide by 0: " + e);
}
}
}
上面的代码生成以下结果。
Java throw语句
我们可以在异常情况下抛出异常。
语法
throw的一般形式如下所示:throw ThrowableInstance;
这里,ThrowableInstance必须是Throwable类型的对象或Throwable的子类。
有两种方法可以获取Throwable对象:在catch子句中使用参数,或者使用new运算符创建一个。
执行流程在throw语句之后立即停止; 任何后续不执行语句。
如何使用Java throws语句?public class Main {
static void aMethod() {
try {
throw new NullPointerException("demo");
} catch (NullPointerException e) {
System.out.println("Caught inside demoproc.");
throw e; // rethrow the exception }
}
public static void main(String args[]) {
try {
aMethod();
} catch (NullPointerException e) {
System.out.println("Recaught: " + e);
}
}
}
上面的代码生成以下结果。
Java throws语句
如果一个方法想抛出一个异常,它必须指定这个行为。
这是包含throws子句的方法声明的一般形式:type method-name(parameter-list) throws exception-list
{
// body of method
}
exception-list是一个逗号分隔的列表,列出了方法可以抛出的异常。public class Main {
static void throwOne() throws IllegalAccessException {
System.out.println("Inside throwOne.");
throw new IllegalAccessException("demo");
}
public static void main(String args[]) {
try {
throwOne();
} catch (IllegalAccessException e) {
System.out.println("Caught " + e);
}
}
}
上面的代码生成以下结果。
Java finally语句
任何代码,将被执行,不管try块放在一个finally阻止。
这是异常处理块的一般形式:try {
// block of code to monitor for errors
}
catch (ExceptionType1 exOb) {
// exception handler for ExceptionType1
}
catch (ExceptionType2 exOb) {
// exception handler for ExceptionType2
}
// ...
finally {
// block of code to be executed after try block ends
}
finally创建一个代码块在try/catch块完成后执行。
即使没有catch语句与异常匹配,finally块也将执行。
finally块可用于关闭文件句柄和释放任何其他资源。finally子句是可选的。public class Main {
// Through an exception out of the method. static void methodC() {
try {
System.out.println("inside methodC");
throw new RuntimeException("demo");
} finally {
System.out.println("methodC finally");
}
}
// Return from within a try block. static void methodB() {
try {
System.out.println("inside methodB");
return;
} finally {
System.out.println("methodB finally");
}
}
// Execute a try block normally. static void methodA() {
try {
System.out.println("inside methodA");
} finally {
System.out.println("methodA finally");
}
}
public static void main(String args[]) {
try {
methodC();
} catch (Exception e) {
System.out.println("Exception caught");
}
methodB();
methodA();
}
}
上面的代码生成以下结果。