Java异常2
异常抛出的两种形式:
- 系统隐式抛出;int n=10/0;—隐式抛出一个异常;
- 手动抛出异常:throw new Exception();
import java.util.InputMismatchException;
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);Student1 st1=new Student1();st1.setId(10);st1.setName("小楼");System.out.println("输入性别");st1.setSex(sc.nextLine());st1.toString();}
}public Student1(String name, int id, double score) {this.name = name;this.id = id;this.sex = sex;}public int getId() {return id;}public String getName() {return name;}public String getScore() {return sex;}public void setId(int id) {this.id = id;}public void setName(String name) {this.name = name;}public void setSex(String sex) {// 方法一,隐式抛出异常// if (!"男".equals(sex)&&!"女".equals(sex)) {// this.sex="男";// } else {// this.sex=sex;// }// 方法二:手动抛出异常if (!"男".equals(sex) && !"女".equals(sex)) {try {// 这种运行异常,程序不要求必须处理throw new RuntimeException();// 手动抛出异常} catch (Exception e) {// TODO: handle exceptionSystem.out.println("输入的性别不合理");}} else {this.sex = sex;}}public String toString() {return "Student [name=" + name + "id=" + id + "score=" + sex + "]";}}
异常处理的两张方式
1、try……catch
2,throws
throws:方法声明异常,如果多个异常类型,用逗号分隔
public void setId(int id) throws InputMismatchException,ArithmeticException{}
import java.util.InputMismatchException;
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);Student1 st1 = new Student1();try {st1.setId(-1);// 此方法声明了异常,主方法进行处理} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}st1.setName("小楼");System.out.println("输入性别");st1.setSex(sc.nextLine());st1.toString();}
}class Student1 {private String name;private int id;private String sex;public Student1() {}public Student1(String name, int id, double score) {this.name = name;this.id = id;this.sex = sex;}public int getId() {return id;}public String getName() {return name;}public String getScore() {return sex;}public void setId(int id) throws Exception{//声明异常if (id>200||id<0) {throw new Exception();}else{}this.id = id;}public void setName(String name) {this.name = name;}public void setSex(String sex) {// 方法一,隐式抛出异常// if (!"男".equals(sex)&&!"女".equals(sex)) {// this.sex="男";// } else {// this.sex=sex;// }// 方法二:手动抛出异常if (!"男".equals(sex) && !"女".equals(sex)) {try {// 这种运行异常,程序不要求必须处理throw new RuntimeException();// 手动抛出异常} catch (Exception e) {// TODO: handle exceptionSystem.out.println("输入的性别不合理");}} else {this.sex = sex;}}public String toString() {return "Student [name=" + name + "id=" + id + "score=" + sex + "]";}}
异常的两种类型
1、RuntimeException;程序不要求必须处理
2、cheked异常,检查异常,程序运行之前必须做好处理措施,否者编译报错,Unhandled Exception type Exception
异常处理机制的好处
1、程序不会中断,处理异常之后可以继续执行
2、可以向用户提供友好的异常信息
3、将程序的正常流程与错误处理分开,有利于代码的编写与维护
/*
*方法的重写:子类声明的异常类型必须和父类一致,或是父类异常类型的子类。
*
*/
/*
*自定义异常:1.继承系统的异常类;
*public class SexException extends Exception {
}2.添加构造方法;public SexException() {}public SexException(String message) {super(message);}
*/
*throw和throws:
*1)所在位置不一样:throw出现在方法体里;
-
throws出现在方法声明处;
*2)后面跟的内容不一样:throw new Exception();异常对象;跟一个对象;
-
throws Exception;异常类型;跟多个异常类型,用逗号分隔;
*3)功能不一样:throw:抛出异常;
-
throws:当作异常处理方式,向上抛出异常。
1.运行时异常:
java.lang.ArrayIndexOutOfBoundsException;
java.lang.NullPointerException;
java.util.InputMismatchException;
java.lang.ArithmeticException;
java.lang.ClassCastException;
2.检查异常:
ClassNotFoundException
IOException