java——详述throw与throws

1、throw抛出的是异常类创建的对象,用于方法或代码块中。

package test1.exception;

public class AgeException extends Exception{//自定义异常类

	public AgeException(String msg) {
		super(msg);
	}
}
package test1;

import test1.exception.AgeException;

public class Student {
	private int age;
	public void setAge(int age){
		if(age<0||age>15) {
			//System.out.println("错误");
			try {
				throw new AgeException("年龄超范围");//抛出异常类创建的对象
			}catch(Exception e) {
				e.toString();
			}
		}
		else {
			this.age=age;
		}
	}
	
	public int getAge() {
		return age;
	}
}
package test1;

import test1.exception.AgeException;

public class Test {

	public static void main(String[] args){
		Student student =new Student();
		student.setAge(12000);
		int age=student.getAge();
		System.out.println(age);
	}
}

2、如果throw抛出的是运行时异常类(RuntimeException)创建的对象,则不需要显式使用throws

package test1.exception;

public class AgeException extends RuntimeException{//运行时异常类

	public AgeException(String msg) {
		super(msg);
	}
}
package test1;

import test1.exception.AgeException;

public class Student {
	private int age;
	public void setAge(int age){
		if(age<0||age>15) {
			throw new AgeException("年龄超范围");//抛出运行时异常类创建的对象
		}
		else {
			this.age=age;
		}
	}
	
	public int getAge() {
		return age;
	}
}
import test1.exception.AgeException;

public class Test {

	public static void main(String[] args){
	
		Student student =new Student();
		student.setAge(12000);
		int age=student.getAge();
		System.out.println(age);
	}
}

3、如果throw抛出的是非运行时异常类创建的对象,要显示使用throws或try-catch
否则会像下图一样出错:
在这里插入图片描述

package test1.exception;

public class AgeException extends Exception{//非运行时异常类

	public AgeException(String msg) {
		super(msg);
	}
}
package test1;

import test1.exception.AgeException;

public class Student {
	private int age;
	public void setAge(int age) throws AgeException{//显式使用throws
		if(age<0||age>15) {
			throw new AgeException("年龄超范围");
		}
		else {
			this.age=age;
		}
	}
	
	public int getAge() {
		return age;
	}
}

-----------或是----------
package test1;

import test1.exception.AgeException;

public class Student {
	private int age;
	public void setAge(int age){
		if(age<0||age>15) {
			try {      //显式使用try-catch
				throw new AgeException("年龄超范围");
			} catch (AgeException e) {
				e.printStackTrace();
			}
		}
		else {
			this.age=age;
		}
	}
	
	public int getAge() {
		return age;
	}
}
package test1;

import test1.exception.AgeException;

public class Test {

	public static void main(String[] args){
	
		Student student =new Student();
		student.setAge(12000);
		int age=student.getAge();
		System.out.println(age);
	}
}

4、

package test1;

public class Test {

  public static void div(int b)throws Exception {
    if(b==0) {
      try {
        throw new Exception("分母不能为0");
      } catch (Exception e) {
        System.out.println("执行");
        e.printStackTrace();
      }
    }
  }
  
  public static void main(String[] args) {
    try {
      div(0);//此时这行可以正常运行
    } catch (Exception e) {//那么就永远不会运行catch里的语句
      System.out.println("永远无法执行");
    }
  }
}

在这里插入图片描述

5、

package test1;

public class Test {
	public static void div(int b)throws Exception {
	    if(b==0) {//当传过来的参数为零时
	      throw new Exception("分母不能为0");//抛出异常
	    }
	}

	public static void main(String[] args){
		try {
		      div(0);//当调用这个方法时,已经在这个方法中抛出异常,说明这行代码无法正常执行
		} catch (Exception e) {
		      System.out.println("永远无法执行变成一定会执行");//无法正常执行try里的语句,才一定会执行catch语句块
		}
	}
}

在这里插入图片描述

发布了29 篇原创文章 · 获赞 3 · 访问量 366

猜你喜欢

转载自blog.csdn.net/qq_44687512/article/details/105447306
今日推荐