try-catch、throws、throw
try-catch
用try-catch捕获到异常后,对其进行处理,不会影响后面程序的执行,因此在程序1捕获并处理除数为0的异常后,依旧以后可以打印irene。
程序1(在内层捕获异常):
public class Test {
public static void main(String[] args) {
int a = 6, b = 0;
divi(a, b);
System.out.print("irene");
}
public static void divi(int a, int b) {
try {
int c = a / b;
System.out.println(c);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
输出:
/ by zero
irene
程序2(内层未捕获在外层捕获异常):
public class Test {
public static void main(String[] args) {
try {
int a = 6, b = 0;
divi(a, b);
} catch(Exception e) {
System.out.println(e.getMessage());
}
System.out.print("irene");
}
public static void divi(int a, int b) {
int c = a / b;
System.out.println(c);
}
}
输出:
/ by zero
irene
程序3(内层捕获异常后,调用者不再捕获):
public class Test {
public static void main(String[] args) {
int a = 6, b = 0;
try{
div(a, b);
} catch(Exception e){
System.out.println("外层"+ e.getMessage());
}
System.out.println("irene");
}
public static void div(int a, int b) {
try {
int c = a / b;
System.out.println(c);
} catch (Exception e) {
System.out.println("内层" + e.getMessage());
}
}
}
输出:
内层/ by zero
irene
throws
可以将不对异常做处理,而是交给调用者去处理,被调用的方法如果有异常可以直接通过throws抛给上层去进行处理。
public class Test {
public static void main(String[] args) {
try {
int a = 6, b = 0;
divi(a, b);
} catch(Exception e) {
System.out.println(e.getMessage());
}
System.out.print("irene");
}
public static void divi(int a, int b) throws Exception{
int c = a / b;
System.out.println(c);
}
}
输出:
/ by zero
irene
throw
如果在被调用的方法中加了try-catch,必须通过throw抛出异常给上层去处理,不能自动将异常抛给上层去处理
throw要和try-catch或者throws搭配起来使用。
程序1:
public class Test {
public static void main(String[] args) {
int a = 6, b = 0;
try {
div(a, b);
} catch (Exception e) {
System.out.println("外层" + e.getMessage());
}
System.out.println("irene");
}
public static void div(int a, int b) {
try {
int c = a / b;
} catch (Exception e) {
throw e;
}
}
}
输出:
外层/ by zero
irene
程序2(和try-catch搭配使用):
public class Test {
public static void main(String[] args) {
int a = 6, b = 0;
try {
div(a, b);
} catch (Exception e) {
System.out.println("外层" + e.getMessage());
}
System.out.println("irene");
}
public static void div(int a, int b) {
int c = a / b;
if(b == 0) {
try {
throw new Exception();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
输出:
外层/ by zero
irene
程序3(和throws搭配使用):
public class Test {
public static void main(String[] args) {
int a = 6, b = 0;
try {
div(a, b);
} catch (Exception e) {
System.out.println("外层" + e.getMessage());
}
System.out.println("irene");
}
public static void div(int a, int b) throws Exception{
int c = a / b;
if(b == 0) {
throw new Exception();
}
}
}
输出:
外层/ by zero
irene
程序4:
public class Test {
public static void div(int a, int b){
if (a == 6) {
throw new IllegalArgumentException("被除数为6");
}
}
public static void main(String[] args) {
int a = 6, b = 2;
try {
div(a, b);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
System.out.println("irene");
}
}
输出:
被除数为6
irene
但是,并不是throw每一种异常,需要和try-catch和throws搭配使用,如NullPointerException异常就是可以单独使用的
自定义异常
public class Test {
public static void main(String[] args) {
int a = 6, b = 0;
try {
div(a, b);
} catch (myException myException) {
System.out.println("自定义异常:" + myException.getMessage());
} catch (Exception e) {
System.out.println("flag");
}
System.out.println("irene");
}
public static void div(int a, int b) throws myException,Exception {
if(a == 6) {
throw new myException("被除数为6");
}
int c = a / b;
}
}
class myException extends Exception{
myException(String s){
super(s);
}
}
输出:
自定义异常:被除数为6
irene