常见异常:
/
/
/
/
/
Throwable//异常父类
//通用陷阱在有其他陷阱存在的情况下只能写在后面
一般发生错误的时候,会先找相应的陷阱,如果没有相应的陷阱,那才进入通用陷阱
/
try {
//System.out.println(2/0);//不能被零整除
//byte[] b=new byte[1024*1024*10000];
}catch (Exception e){//通用陷阱;只要是Exception的子类全都可以陷进去
System.out.println("e+"+e);
}catch (Error e){//通用陷阱只要是Error的子类全都可以陷进去
System.out.println("e+"+e);
}catch (Throwable e){//通用陷阱;这个啥都可以陷
System.out.println("e+"+e);
}
/
/
System.out.println("1");
try {//把可能出现的错误写在try里面
System.out.println("2");
String s=null;
System.out.println("3");
s.toString();
System.out.println("4");
int[] num=new int[2];
System.out.println("5");
num[2]=100;
System.out.println("6");
}catch (NullPointerException e){
System.out.println("7");
}catch (ClassCastException e){
System.out.println("8");
}catch (ArithmeticException e){
System.out.println("9");
}
System.out.println("10");//打印1 2 3 7 10
/
/
try {
int [] num=new int[2];
num[2]=100;
}catch (NullPointerException e){
System.out.println(e);
}catch (ArithmeticException e){
System.out.println(e);
}catch (ClassCastException e){
System.out.println(e);
}catch (NumberFormatException e){
System.out.println(e);
}catch (Exception e){//打印java.lang.ArrayIndexOutOfBoundsException: 2
System.out.println(e);
}
/
/
System.out.println("1");
try {
System.out.println("2");
String s=null;
s.toString();
System.out.println("3");
}catch (NullPointerException e){
System.out.println("4");
}catch (Exception e){
System.out.println("5");
}
System.out.println("6");//打印1246
/
/
System.out.println("1");
try {
System.out.println("2");
String s="";
s.toString();
System.out.println("3");
int[] i=new int[2];
i[2]=23;
}catch (NullPointerException e){
System.out.println("4");
}catch (Exception e){
System.out.println("5");
}
System.out.println("6");//打印12356
/
/
System.out.println("1");
try {
try {
try {
System.out.println("2");
int[] i=new int[2];
i[-1]=12;
}catch (NullPointerException e){
System.out.println("3");
}
System.out.println("4");
}catch (Error e){
System.out.println("5");
}
System.out.println("6");
}catch (Throwable e){
System.out.println("7");
}
System.out.println("8");//打印1278
/
System.out.println("1");
try {
try {
try {
System.out.println("2");
int[] i=new int[2];
i[-1]=12;
}catch (NullPointerException e){
System.out.println("3");
}
System.out.println("4");//如果代码正确了才会执行这条
}catch (Exception e){
System.out.println("5");
}
System.out.println("6");
}catch (Throwable e){
System.out.println("7");
}
System.out.println("8");//打印12568
/
/
/
/
finally //最终执行快 //一定会执行只能写在catch后面
当错误没有得到解决时,直接抛出
/
/
/
/
/
/
/
自定义异常
1、抛出异常以及方法抛异常
//throw 动作抛异常给陷阱
//throws 在方法上声明要抛的异常
/
public class Test3 {
//可能会发生空指针错误
public static void method() throws NullPointerException,ClassNotFoundException{
// throw new NullPointerException();
throw new ClassNotFoundException();
}
public static void main(String[] args) {
try {
method();
throw new NullPointerException();//抛异常
}catch (NullPointerException e){
System.out.println("处理"+e);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//直接继承Exception 或者Error 他们都得实现try catch 或者方法抛出
// RintimeException 是不需要的
//Exception 会强制要求你处理
//RintimeException 不会强制要求你处理此异常
}
}
/
/
package jichu10;
//自定义异常
public class AgeOutException extends RuntimeException {
public AgeOutException(){}
public AgeOutException(String message){
super(message);
}
}
/
package jichu10;
public class Student {
private int age;
public int getAge() {
return age;
}
public void setAge(int age) throws AgeOutException{
if (age<=0||age>=150){
throw new AgeOutException("年龄范围是1至150岁");//抛出异常
}
this.age = age;
}
public static void main(String[] args) {
Student s=new Student();
s.setAge(170);
}
}/*Exception in thread "main" jichu10.AgeOutException: 年龄范围是1至150岁
at jichu10.Student.setAge(Student.java:12)
at jichu10.Student.main(Student.java:19)
*/
/
package jichu10;
public class Tset4 {
public static void main(String[] args) {
try {
throw new AgeOutException("小年轻");
}catch (Exception e){
//System.out.println(e.getMessage());
e.printStackTrace();
}//写try catch的时候最好要就代码
}
}/*jichu10.AgeOutException: 小年轻
at jichu10.Tset4.main(Tset4.java:6)
*/