- 异常处理方法
- 多个异常处理(多个catch)
- Finally的用法
- 使用 catch 处理异常
public class Demo {
public static void main(String[] args) {
demo4();
}
public static void demo1() {
try {
throw new Exception("My Exception");
} catch (Exception e) {
System.err.println("Caught Exception");
System.err.println("getMessage():" + e.getMessage());
System.err.println("getLocalizedMessage():" + e.getLocalizedMessage());
System.err.println("toString():" + e);
System.err.println("printStackTrace():");
e.printStackTrace();
}
}
public static void demo2() {
Test t = new Test();
try {
int x = t.div(5, 0);
System.out.println("x=" + x);
} catch (ArithmeticException e) {
System.out.println(e.toString());
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e.toString());
} catch (Exception e) {
System.out.println(e.toString());
}
}
public static void demo3() {
Object o = null;
for (int i = 0; i < 5; i++) {
try {
o = makeObj(i);
} catch (IllegalArgumentException e) {
System.err.println("Error: (" + e.getMessage() + ").");
return;
} finally {
System.err.println("都已执行完毕");
if (o == null)
System.exit(0);
}
System.out.println(o);
}
}
public static Object makeObj(int type) throws IllegalArgumentException {
if (type == 1)
throw new IllegalArgumentException("不是指定的类型: " + type);
return new Object();
}
public static void demo4() {
int array[] = { 20, 20, 40 };
int n1 = 15, n2 = 10;
int result = 10;
try {
result = n1 / n2;
System.out.println("结果为:" + result);
for (int i = 5; i >= 0; i--) {
System.out.println("数组的元素值为:" + array[i]);
}
} catch (Exception e) {
System.out.println("触发异常:" + e);
}
}
}
class Test {
int div(int a, int b) throws ArithmeticException, ArrayIndexOutOfBoundsException {
int[] arr = new int[a];
System.out.println(arr[4]);
return a / b;
}
}
class Demo {
public static void main(String[] args) {
demo3();
}
public static void demo1() {
MyThread t = new MyThread();
t.start();
try {
Thread.sleep(1000);
} catch (Exception x) {
System.out.println("Caught it" + x);
}
System.out.println("Exiting main");
}
public static void demo2() {
int array[] = { 20, 20, 40 };
int num1 = 15, num2 = 10;
int result = 10;
try {
result = num1 / num2;
System.out.println("The result is" + result);
for (int i = 5; i >= 0; i--) {
System.out.println("The value of array is" + array[i]);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void demo3() {
int n = 20, result = 0;
try {
result = n / 0;
System.out.println("结果为" + result);
} catch (ArithmeticException ex) {
System.out.println("发算术异常: " + ex);
try {
throw new NumberFormatException();
} catch (NumberFormatException ex1) {
System.out.println("手动抛出链试异常 : " + ex1);
}
}
}
}
class MyThread extends Thread {
public void run() {
System.out.println("Throwing in " + "MyThread");
throw new RuntimeException();
}
}
class Demo {
double method(int i) throws Exception {
return i / 0;
}
boolean method(boolean b) {
return !b;
}
static double method(int x, double y) throws Exception {
return x + y;
}
static double method(double x, double y) {
return x + y - 3;
}
public static void main(String[] args) {
Demo d = new Demo();
try {
System.out.println(method(10, 20.0));
System.out.println(method(10.0, 20));
System.out.println(method(10.0, 20.0));
System.out.println(d.method(10));
} catch (Exception e) {
System.out.println("exception occoure: " + e);
}
}
}
class Demo {
public static void main(String[] args) {
try {
new Input().method();
} catch (WrongInputException wie) {
System.out.println(wie.getMessage());
}
}
}
class WrongInputException extends Exception {
WrongInputException(String s) {
super(s);
}
}
class Input {
void method() throws WrongInputException {
throw new WrongInputException("Wrong input");
}
}