标签:空指针异常 abs abstract this ret class 电脑 模块 16px
Java中的异常,就是对不正常情况进行描写叙述后的对象体现。
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 {// 关闭资源}}}
假设多个catch块中的异常出现继承关系,父类异常catch块放最后。
RuntimeException及其子类除外。
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放假!"
);}}}}
标签:空指针异常 abs abstract this ret class 电脑 模块 16px
原文地址:http://www.cnblogs.com/mthoutai/p/7222259.html