当前位置:网站首页>详述throw与throws
详述throw与throws
2022-07-27 12:32:00 【涟涟涟涟】
引入
封装引入:
看下面的代码:
package keeper;
public class Student {
public int age1 =89;
private int age =23;
public void setAge(int age ) {
if(age>29&&age<879) {
this.age=age;
}else {
System.out.println("年龄无效");
}
}
public int getAge( ) {
return age;
}
}
package keeper;
public class Test {
public static void main(String [] args) {
new Student().age1=5857324;
System.out.println(new Student().age1);
Student student =new Student();
student.setAge(852); //fuzhi
System.out.println(student.getAge());
}
}
结果:
89
852
因为age1变量为public级别的变量,任何人都可以使用,则定义Student类的人无法对该变量的范围进行有效的约束 。然而age变量则是private类型的。即:公共方法操作私有属性。
思考:System.out.println(“年龄无效”);即输出这种提示错误的方式对于调用该方法的程序员没有什么过多的直接的帮助。

解决方案:throw 异常对象
这样写虽然说也行,但是他报错报的是空指针异常,而我们想要他报错报的是年龄异常。给人一种词和意思不搭的别扭感。所以,我们要另外写一个年龄异常放在这里,然后当我们写错的时候,程序就会报年龄异常这个错误,而不是空指针异常。
所以,我们采用下面的方法:
自定异常:
为什么?
Java API提供的已有异常类无法准确表述当前发生的异常问题,这时就需要创建自定义的异常。
怎么做?
创建继承Exception 或其子类的自定义类;
自定义异常类调用父类构造函数(通常是含有一个String类型参数的构造函数);
例:
package keeper;
import cn.jd.vo.AgeException;
public class Student {
public int age1 =89;
private int age =23;
public void setAge(int age ) {
if(age>29&&age<879) {
this.age=age;
}else {
//System.out.println("年龄无效");
//throw new NullPointerException("年龄无效");
throw new AgeException("年龄无效");
}
}
public int getAge( ) {
return age;
}
}
package keeper;
public class Test {
public static void main(String [] args) {
new Student().age1=5857324;
System.out.println(new Student().age1);
Student student =new Student();
student.setAge(8); //赋值
System.out.println(student.getAge());
}
}
package cn.jd.vo;
public class AgeException extends RuntimeException{
private static final long serialVersionUID = 7652138953038774791L;
public AgeException(String message) {
super(message);
}
}

这样的话,对于另一个人而言,他就知道了,程序报错,什么错了呢?哦,年龄,在哪一行呢?然后就很清楚明白。
自定异常分类:
将自定异常抛给方法调用者:
package keeper;
import cn.jd.vo.AgeException;
public class Student {
public int age1 =89;
private int age =23;
public void setAge(int age ) {
if(age>29&&age<879) {
this.age=age;
}else {
throw new AgeException("年龄无效");
}
}
public int getAge( ) {
return age;
}
}

至于方法调用者是再抛给别人,还是抛给自己,那就不关我们的事情了。
将自定异常抛给自己:
package keeper;
import cn.jd.vo.AgeException;
public class Student {
public int age1 =89;
private int age =23;
public void setAge(int age ) {
if(age>29&&age<879) {
this.age=age;
}else {
try {
throw new AgeException("年龄无效");
}catch(AgeException e) {
e.printStackTrace();
}
}
}
public int getAge( ) {
return age;
}
}

throw
throw用于抛出异常对象;
1、如果异常对象为运行时,
则
方法参数列表后面可以不使用throws,不使用try-catch。
也可以将异常抛给方法调用者;
例:
package keeper;
import cn.jd.vo.AgeException;
public class Student {
public int age1 =89;
private int age =23;
public void setAge(int age ) {
if(age>29&&age<879) {
this.age=age;
}else {
throw new AgeException("年龄无效");
}
}
public int getAge( ) {
return age;
}
}
2、如果异常对象为检查时,
则
方法参数列表后面必须使用throws抛出创建该对象的类;
如果没有throws 必须用try-catch。
例:


用throws:
package keeper;
import cn.jd.vo.AgeException;
public class Student {
public int age1 =89;
private int age =23;
public void setAge(int age ) throws AgeException {
if(age>29&&age<879) {
this.age=age;
}else {
throw new AgeException("年龄无效");
}
}
public int getAge( ) {
return age;
}
}
用try-catch:
public class Student {
public int age1 =89;
private int age =23;
public void setAge(int age ) {
if(age>29&&age<879) {
this.age=age;
}else {
try {
throw new AgeException("年龄无效");
} catch (AgeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public int getAge( ) {
return age;
}
}
throws

区别:
抛出的东西不同:
throw抛出的是具体的异常对象,
而
throws抛出的是抽象的异常类;
使用位置不同:
throw一般用在方法体中,也可用在代码块中,但是如果抛出的是检查时异常类创建的对象,则必须使用try-catch自行处理;
throws只能用在方法声明括号后面;
throw用在代码块中:
{
int a=12;
if(a==12) {
throw new AgeException("");
}
}

边栏推荐
- MySQL paging query instance_ MySQL paging query example explanation "suggestions collection"
- The song of the virtual idol was originally generated in this way!
- Bishi journey
- Recursive method | Fibonacci sequence
- Unity Shader 一 激光特效Shader[通俗易懂]
- Fundamentals of mathematics 01
- The strongest distributed locking tool: redisson
- Guangdong's finance has taken many measures to help stabilize the "ballast stone" of food security
- Wechat applet must use interface "suggestions collection"
- 多表查询
猜你喜欢

20210519 leetcode double pointer

Finally, I was ranked first in the content ranking in the professional field. I haven't been tired in vain during this period. Thanks to CSDN's official platform, I'm lucky and bitter.

评价自动化测试优劣的隐性指标

How to use the server to build our blog

Chapter 13 IO flow

Bishi journey

A personal blog integrating technology, art and sports.

虚拟偶像的歌声原来是这样生成的!
Several rounds of SQL queries in a database

XXL job parameter transfer
随机推荐
Redistemplate cannot get the value according to the key
Write and read system temporary files: createtempfile and tempfilecontent[easy to understand]
When the script runs in the background, it redirects the log from the console to its own named file
Kazoo tutorial
微信小程序必用接口「建议收藏」
Unity Shader 一 激光特效Shader[通俗易懂]
CLS 监控告警:实时保障线上服务高可用性
开关量输入输出模块DAM-5055
No matching distribution found for flask_ A solution to compat
20210408 longest public prefix
5V boost 9V chip
You haven't connected to the proxy server. There may be a problem or the address is incorrect (how to check the proxy server IP)
Wechat applet session holding
Chapter 12 generics
Map接口
[excerpt] [medical image] common DICOM thumbnail interpretation and viewer converter conversion tool
[网摘][医学影像] 常用的DICOM缩略图解释以及Viewer converter 转换工具
J9 number theory: how long is the mainstreaming of decentralized identity?
Several rounds of SQL queries in a database
Complete data summary of lapsus$apt organization that stole Microsoft's source code in March 2022