/**
测试异常处理
*/
public class TestException {
public static void main(String[] args) {
//除数为0导致的错误
int a = 0;
int b = 1;
if(a!=0) {
System.out.println(b/a);
}
//空指针导致的错误
String str = null;
if(str!=null) {
System.out.println(str.length());
}
//转类型导致的错误
Animal d = new Dog();
if(d instanceof Cat) { //instanceof是Cat类型就转,不是就不执行
Cat c = (Cat)d;
}
}
}
class Animal {}
class Dog extends Animal {}
class Cat extends Animal {}