什么是异常:




异常处理方式一:try-catch的使用:

Java异常处理的方式:
方式一:try-catch-finally
方式二:throws+异常类型
方式一:捕获异常(try-catch-fianlly)





异常处理方式二:throws

举例:


一层一层往上抛,然后再某一个方法中try-catch-finally。这个异常就被处理了
抛到main方法中就到头了,不能再往上throws,必须再main里try-catch

4.方法的重写的要求(针对编译时的异常来说)
子类重写的方法抛出的异常类型可以与父类被重写的方法抛出的异常类型相同,或是父类被重写的方法抛出的异常类型的子类
举例:
public class ErrorTtest {
}
class Father{
public void method1() throws IOException {
}
}
class Son extends Father{
public void method1 ()throws FileNotFoundException {//这个异常类型只能和父类一样或者是父类异常类型的子类
}
}

使用throw手动抛出异常:


举例:


如何自定义异常类和课后练习:

举例:
public class BelowZeroException extends Exception {
static final long serialVersionUID = 2134455345671L;
public BelowZeroException() {
}
public BelowZeroException(String name) {
super(name);
}
public BelowZeroException(String message, Throwable cause) {
super(message, cause);
}
}


练习1:
自定义异常类:
public class NoLifeValueException extends RuntimeException {
static final Long serialVersionUID = 34565445671L;
public NoLifeValueException(){
}
public NoLifeValueException(String message) {
super(message);
}
}
Person类:
public class Person {
private String name;
private int lifeValue;//生命值
public Person() {
}
public Person(String name, int lifeValue) {
this.name = name;
setLifeValur(lifeValue);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getLifeValur() {
return lifeValue;
}
public void setLifeValur(int lifeValur) throws NoLifeValueException {
if(lifeValur < 0){
throw new NoLifeValueException("生命值不能为负数" + lifeValur);
}
this.lifeValue = lifeValur;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", lifeValue=" + lifeValue +
'}';
}
}
测试类:
public class Exer3 {
public static void main(String[] args) {
try {
Person person = new Person("妄竹", -10);
System.out.println(person);
}catch (NoLifeValueException e){
System.out.println(e.getMessage());
}
System.out.println();
//2.使用空参构造器
Person person1 = new Person();
person1.setName("妄汐霜");
person1.setLifeValur(10);
System.out.println(person1);
}
}
练习2:
自定义异常类型:
public class BelowZeroException extends Exception {
public BelowZeroException(){
}
public BelowZeroException(String message) {
super(message);
}
}
测试类:
public class DivisionDemo {
public static void main(String[] args) {
int m,n,result;
Scanner sc = new Scanner(System.in);
try {
System.out.println("输入m和n的值:");
m = sc.nextInt();
n = sc.nextInt();
System.out.println(result = divide(m, n));
} catch (BelowZeroException e) {
e.printStackTrace();
}catch (NumberFormatException e) {
e.printStackTrace();
System.out.println("数据类型不一致");
}catch (ArrayIndexOutOfBoundsException e){
e.printStackTrace();
System.out.println("缺少命令行参数");
}catch (ArithmeticException e){
e.printStackTrace();
System.out.println("除0");
}
}
public static int divide(int m, int n) throws BelowZeroException {
if (m < 0 || n < 0) {
throw new BelowZeroException("输入负数了");
}
return m / n;
}
}
真题:
运行时异常和一般异常有何异同?


子类重写父类抛出异常的方法,不能抛出比父类更高级别的异常类,只能抛出和父类一样的异常类,或者是父类异常类的子类
863

被折叠的 条评论
为什么被折叠?



