网站备案信息可以改吗用wordpress做论坛
web/
2025/9/26 22:43:07/
文章来源:
网站备案信息可以改吗,用wordpress做论坛,关键词排名优化技巧,学ui设计培训学校异常就是代表程序出现的问题
父类#xff1a;Exception
编译时异常#xff1a;没有继承RuntimeException的异常#xff0c;直接继承于Exception。编译阶段就会错误提示。
运行时异常#xff1a;RuntimeException本身和子类。编译阶段没有错误提示#xff0c;运行时出现…异常就是代表程序出现的问题
父类Exception
编译时异常没有继承RuntimeException的异常直接继承于Exception。编译阶段就会错误提示。
运行时异常RuntimeException本身和子类。编译阶段没有错误提示运行时出现。
作用
1.异常是用来查询bug的关键参考信息
2.异常可以作为方法内部的一种特殊的返回值以便通知调用者底层的执行情况。
throws:
注意写在方法定义处表示声明一个异常告诉调用者使用本方法可能会有那些异常。 编译时异常必须要写 运行时异常可以不写
throw:
注意:写在方法内结束方法手动抛出异常对象交给调用者方法中下面的代码不在执行。
练习
需求:
package myFunction;public class GirlFriend {private String name;private int age;public GirlFriend() {}public GirlFriend(String name, int age) {this.name name;this.age age;}public String getName() {return name;}public void setName(String name) {if(name.length()3||name.length()10){throw new RuntimeException();}this.name name;}public int getAge() {return age;}public void setAge(int age) {if(age18||age40){throw new RuntimeException();}this.age age;}Overridepublic String toString() {return GirlFriend{ name name \ , age age };}
}
package myFunction;
import java.util.Scanner;
public class Test {public static void main(String[] args) {Scanner scnew Scanner(System.in);GirlFriend gfnew GirlFriend();while (true) {try {System.out.println(姓名);String name sc.nextLine();gf.setName(name);System.out.println(年龄);String ageStrsc.nextLine();int ageInteger.parseInt(ageStr);gf.setAge(age);break;} catch (NumberFormatException e) {System.out.println(年龄的格式有误请输入数字);continue;}catch (RuntimeException e) {System.out.println(姓名的长度或者年龄的范围有误);continue;}}System.out.println(gf);}
}自定义异常
步骤
1.定义异常类
2.写继承关系
3.空参构造
4.带参构造
意义就是为了让控制台的报错信息更加的见名知意。
package myFunction;public class NameFormatException extends RuntimeException{public NameFormatException() {}public NameFormatException(String message) {super(message);}
}
package myFunction;public class AgeOutOfBounds extends RuntimeException{public AgeOutOfBounds() {}public AgeOutOfBounds(String message) {super(message);}
}
package myFunction;public class GirlFriend {private String name;private int age;public GirlFriend() {}public GirlFriend(String name, int age) {this.name name;this.age age;}public String getName() {return name;}public void setName(String name) {if(name.length()3||name.length()10){throw new NameFormatException(name格式有误,长度应该为:3-10);}this.name name;}public int getAge() {return age;}public void setAge(int age) {if(age18||age40){throw new AgeOutOfBounds(age超出了范围);}this.age age;}Overridepublic String toString() {return GirlFriend{ name name \ , age age };}
}
package myFunction;import java.util.Scanner;public class Test {public static void main(String[] args) {Scanner scnew Scanner(System.in);GirlFriend gfnew GirlFriend();while (true) {try {System.out.println(姓名);String name sc.nextLine();gf.setName(name);System.out.println(年龄);String ageStrsc.nextLine();int ageInteger.parseInt(ageStr);gf.setAge(age);break;} catch (NumberFormatException e) {e.printStackTrace();continue;}catch (AgeOutOfBounds e) {e.printStackTrace();continue;}catch (NameFormatException e) {e.printStackTrace();continue;}}System.out.println(gf);}
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/82421.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!