异常

 

 

1.ArithmeticException 算术异常

2.ArrayIndexOutOfBoundsException 下标越界异常

3.NullPointerException 空指针异常

4.ClassNotFoundException 类找不到异常

5.NumberFormatException 数字转换异常,比如字符串转换数字

6.InputMismatchhException 类型不匹配异常

RunTimeException运行时异常可以不进行处里

 

自定义异常,指由人为抛出的异常,使用throw关键字

例子:

package example;
import java.util.Scanner;
public class Person {
    // 输入姓名
    Scanner input = new Scanner(System.in);
    String name = input.next();

    private String sex;

    public String getSex() {
        return sex;
    }
    // 为人性别赋值
    public void setSex(String sex) throws Exception {
        if (sex.equals("") || sex.equals("")) {
            this.sex = sex;
        } else {
            throw new Exception("性别只能是男或者女");
        }

    }

}

测试类

package example;

public class demo {
    public static void main(String[] args) {
        Person p =new Person();
        try {
            p.setSex("人妖");
        } catch (Exception e) {
            System.out.println("性别有误!!");
            e.printStackTrace();
        }
    }
}

运行效果

性别有误!!java.lang.Exception: 性别只能是男或者女

    at example.Person.setSex(Person.java:18)
    at example.demo.main(demo.java:7)

 例子2:

package example;
/**
 * 
 * 自定义异常继承Exception类 
 *
 */

public class GendorException extends Exception {
    
    public GendorException(){
        super();
    }
    
    public GendorException(String msg){
        super(msg);
    }
    
}

person类

package example;
import java.util.Scanner;
public class Person {
    // 输入姓名
    Scanner input = new Scanner(System.in);
    String name = input.next();

    private String sex;

    public String getSex() {
        return sex;
    }
    // 为人性别赋值
    public void setSex(String sex) throws GendorException {
        if (sex.equals("") || sex.equals("")) {
            this.sex = sex;
        } else {
            throw new GendorException("性别只能是男或者女");
        }

    }
    
}

测试类

package example;

public class demo {
    public static void main(String[] args) {
        Person p =new Person();
        try {
            p.setSex("人妖");
        } catch (GendorException e) {
            System.out.println("性别有误!!");
            e.printStackTrace();
        }
    }
}

异常链:减少代码关联,不丢失异常信息。可以看出由哪个异常引起的另一个异常

try{
//会抛出异常的代码
}catch(xxxException){
    throw new XXXException(e);
}

Throwable是所有异常的父类。
自定义异常2:

package example;
/**
 * 自定义异常
 *
 */
public class MyException extends Exception {
    public MyException(String msg,Throwable cause){
        super(msg,cause);
    }
}

测试类

package example;

public class test {

    public static void main(String[] args) throws MyException {
        
        try {
            throw new Exception("Exception类型异常");
        } catch (Exception e) {
            throw new MyException("自定义的异常类型", e);
        }

    }

}

运行结果:由Exception引起的自定义异常

Exception in thread "main" example.MyException: 自定义的异常类型
    at example.test.main(test.java:10)
Caused by: java.lang.Exception: Exception类型异常
    at example.test.main(test.java:8)

 

posted @ 2015-04-19 15:58  justlgx  阅读(202)  评论(0编辑  收藏  举报