/**
* @author Endless
*
*/
public class UseExpection {
public static void main(String[] args) {
UseExpection ex = new UseExpection();
try {
ex.Ex();
} catch (Exception e) {
System.out.println("main, End of Catch");
e.printStackTrace();
}
}
public UseExpection() {
}
@SuppressWarnings("finally")
boolean Ex() throws Exception {
boolean ret = false;
try {
ret = Ex1();
} catch (Exception e) {
e.printStackTrace();
ret = false;
System.out.println("Ex,End of Catch ");
Exception xxx = new Exception("Ex 233");
throw xxx;
} finally {
System.out.println("Ex, finally; return value=" + ret);
return ret;
}
}
private boolean Ex1() throws Exception {
boolean ret = true;
try {
ret = Ex2();
if (!ret) {
return false;
}
System.out.println("Ex1,end of Try");
return ret;
} catch (Exception e) {
e.printStackTrace();
System.out.println("Ex1,end of Catch");
ret = false;
Exception xxx = new Exception("Ex1 233");
throw xxx;
}
}
private boolean Ex2() throws Exception {
boolean ret = true;
try {
int b = 12;
int c;
for (int i = 2; i >= -1; i--) {
c = b / i;
System.out.println("i=" + i + "c=" + c);
}
return true;
} catch (Exception e) {
System.out.println("Ex2,End of Catch");
ret = false;
Exception xxx = new Exception("Ex2 233");
throw xxx;
}
}
}
自定义异常
import java.util.InputMismatchException;
import java.util.Scanner;
public class MyScanner {
private static Scanner in;
public static void main(String[] args) {
sss();
}
private static void sss() {
Integer x = 0;
Integer y = 0;
in = new Scanner(System.in);
System.out.println("请输入一个数字");
try {
x = in.nextInt();
System.out.println("请输入一个整数:");
y = in.nextInt();
System.out.println("x/y=" + x / y);
} catch (InputMismatchException e) {
try {
throw new InputMismatchException("必须输入一个int值范围的数字");
} catch (Exception e2) {
e2.printStackTrace();
}
} catch (Exception e) {
if (y == 0) {
try {
throw new Exception("除数不能为零!");
} catch (Exception e3) {
e3.printStackTrace();
}
}
}
}
}
public class MyException extends Exception {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
try {
throw new MyException("自定义异常!");
} catch (MyException e) {
e.printStackTrace();
}
}
public MyException() {
super();
}
public MyException(String message) {
super(message);
}
}