标签:
class Div {int div(int a, int b) throws Exception // 必须对其的调用进行捕获或声明以便抛出{return a / b;}int MultiEx(int a, int b) throws ArithmeticException, ArrayIndexOutOfBoundsException {int c = a / b;int[] arr = new int[a];System.out.println(arr[a]);return c;}}public class ExceptionDemo {public static void main(String[] args) // throws Exception{Div d = new Div();try {int x = d.div(2, 0);System.out.println("x = " + x);} catch (Exception e) {System.out.println("异常!");System.out.println(e.getMessage()); // 异常信息System.out.println(e.toString()); // 异常名称:异常信息e.printStackTrace(); // 异常名称:异常信息// 异常出现的位置}System.out.println("----------------");try {int x = d.MultiEx(4, 1);System.out.println("x = " + x);} catch (ArithmeticException e) {System.out.println("除数不能为0!");System.out.println(e.toString());} catch (ArrayIndexOutOfBoundsException e) {System.out.println("数组下标越界!");System.out.println(e.toString());} catch (Exception e) {System.out.println(e.toString());}System.out.println("----------------");}}
try{throw new AException();}catch(AException e){throw e;}
try{throw new AException();}catch(AException e){//对AException处理。throw new BException;}
class Demo1 {void func() throws Exception {// 异常处理:抛出throw new Exception();}}class Demo2 {void func() {// 异常处理:捕捉try {throw new Exception();} catch (Exception e) {}}}class Demo3 {void func() throws Exception {try {throw new Exception();} catch (Exception e) {// 捕捉异常,如果无法处理,可以继续抛出ethrow e;}}}class Demo4 {void func() {try {throw new Exception();} catch (Exception e1) {// 捕捉异常,如果无法处理,可以将异常e1转化成调用者可接受异常后抛出try {throw e1;} catch (Exception e2) {// 异常转换}}}}class Demo5 {void func() throws Exception {try {throw new Exception();} finally {// 关闭资源}}}
class MyException extends Exception{MyException(String msg){super(msg);}}
/** 自定义异常继承体系* Exception* |--AException* | |--BException* |--CException*/class AException extends Exception {}class BException extends AException {}class CException extends Exception {}class Father {void func() throws AException {}}class Son extends Father {void func() throws BException {// 只能抛出AException或者AException的子类BException,不能抛出CException// 如果子类产生新异常CException,这里只能try,不能抛出。}}
class NegativeException extends Exception {private int value;NegativeException(String msg) {super(msg);}NegativeException(String msg, int value) {super(msg);this.value = value;}int getValue() {return value;}}class Demo {int customExc(int x) throws NegativeException // 函数内手动抛出非运行时异常,必须对其进行捕捉或声明抛出。{if (x < 0) throw new NegativeException("负数!", x);return x;}int runtimeExc(int x) // 函数内手动抛出RuntimeException异常或其子类异常,不需要对其进行捕捉或声明抛出。{if (x == 0) throw new ArithmeticException("数值为0!");return x;}void checkString(String s) {if (s.equals("String"))// 避免空指针异常,应修改为:// if("String".equals(s))System.out.println("PASS!");elseSystem.out.println("FAIL!");}}public class ExceptionCustom {public static void main(String[] args) // throws Exception{Demo d = new Demo();try {int x = d.customExc(-3);System.out.println("x = " + x);} catch (NegativeException e) {System.out.println(e.toString() + "\n该数为:" + e.getValue());}System.out.println("-------------------");d.runtimeExc(0); // 算术异常(运行时),停止程序,需要程序员修改代码。System.out.println("-------------------");d.checkString("String");d.checkString(null); // 空指针异常(运行时)。System.out.println("-------------------");}}
// 员工使用电脑案例/*** 电脑死机异常*/class CrashException extends Exception {CrashException(String msg) {super(msg);}}/*** 电脑烧毁异常*/class BurnException extends Exception {BurnException(String msg) {super(msg);}}/*** 无法工作异常*/class WorkException extends Exception {WorkException(String msg) {super(msg);}}class Computer {// 电脑状态,0电脑正常,1电脑死机,2电脑烧毁private int state = 0;public void run() throws CrashException, BurnException {if (state == 1) throw new CrashException("电脑崩溃了!");if (state == 2) throw new BurnException("电脑烧毁了!");System.out.println("电脑运行...");}public void reboot() {System.out.println("电脑重启...");state = 0;}public void setState(int state) {this.state = state;}}abstract class Employee {private String name;Employee(String name) {this.name = name;}abstract void work() throws WorkException;}class Staff extends Employee {private Computer com;Staff(String name) {super(name);com = new Computer();}public void work() throws WorkException {try {com.run();} catch (CrashException e) {// 如果电脑死机了,则重启电脑即可System.out.println(e.toString());com.reboot();} catch (BurnException e) {// 如果电脑烧毁了,则向上级报告,抛出无法工作异常请求放假。throw new WorkException("无法继续工作!\n原因:" + e.toString());}System.out.println("工作!");}public void computerStateChange(int state) {com.setState(state);}}public class ExceptionCase {public static void main(String[] args) {Staff s = new Staff("Jacob");// 分别针对不同的电脑状态,模拟工作情况for (int i = 0; i < 3; i++) {System.out.println("------------------情况" + i + ":------------------");// 更改员工电脑的状态s.computerStateChange(i);// 员工工作try {s.work();} catch (WorkException e) {System.out.println(e.toString() + "\n放假!");}}}}
标签:
原文地址:http://blog.csdn.net/u010388781/article/details/51167720