技术标签: java
异常复习:
<1>
public static void main(String[] args) {
Student stu = new Student();
try {
stu.setAge(250);//是可能出现异常的代码
}catch(Exception e) {
System.err.println(e.getMessage());//只获得报错的原因即可
}
try {
stu.setSex("未知");//受查异常,编译期间就报错,需要处理
}catch(SexMismatchException se) {
//根据方法声明的异常,捕获相应的类型
System.err.println(se.getMessage());
}catch(Exception e ) {
e.printStackTrace();
}
// Class.forName("xxx.xxx");//参数(包名.类名) 可能写错
}
}
class SexMismatchException extends Exception{
public SexMismatchException() {
}
public SexMismatchException(String message) {
super(message);
}
}
class AgeInputException extends RuntimeException{
public AgeInputException() {
}//支持创建无异常原因信息的异常对象
public AgeInputException(String message) {
//提供有参构造方法,支持编写异常原因信息
super(message);//调用父类的有参构造方法,为message属性赋值。
}
}
class Student{
private int age;
private String sex;
public void setSex(String sex)throws SexMismatchException{
if(sex.equals("男")||sex.equals("女")) {
this.sex=sex;
}else {
throw new SexMismatchException("性别输入的值为:“男”或者“女”");
}
}
public String getSex() {
return this.sex;
}
public int getAge() {
return this.age;
}
public void setAge(int age){
if(age > 0 && age < 123) {
this.age = age;
}else {
throw new AgeInputException("年龄的赋值应该在0岁到123岁之间");//抛运行时异常的父类。 不合理。现存的定义好的异常,没有符合现在程序的场景
}
}
}
输出参考:
年龄的赋值应该在0岁到123岁之间
性别输入的值为:“男”或者“女”
<2>
public static void main(String[] args) {
System.out.println(ma());
System.out.println("请输入您的用户名:");
}
public static int ma() {
int b = 0;
//读入一个整数b;
try {
int n = 100;
return n / b;
}catch(Exception e) {
return 10;
}finally {
System.out.println("程序结束了!");
}
}
}
输出参考:
程序结束了!
10
请输入您的用户名:
<3>
public class TestOverrideExceptionMethod {
public static void main(String[] args) {
Super sup = new Sub();//父类引用指向子类对象 多态
try {
sup.method();//在编译期间,调用的父类中声明的方法是有异常的,需要处理
}catch(Exception e) {
e.printStackTrace();//处理方案之1:打印堆栈跟踪信息
}
}
}
class Super{
public void method()throws Exception{
System.out.println("method in Super");
}
}
class Sub extends Super{
public void method()throws ClassNotFoundException,RuntimeException,IOException,SQLException{
System.out.println("method in Sub");
}
}
interface Printable{
public void print()throws Exception;
}
class MyClass implements Printable{
public void print() throws ClassNotFoundException,RuntimeException{
}
}
输出参考:
method in Sub
<4>
public static void main(String[] args) {
int result = m1();
System.out.println(result);
}
public static int m1() {
int a = 10;
try {
a = 20;
throw new RuntimeException();
}catch(Exception e) {
a = 30;
return a;
}finally {
a = 40;
}
}
}
输出参考:
30
文章浏览阅读757次,点赞19次,收藏9次。文字动态特效_html canvas 效果
针对组件业务上的需求,需要给 el-table-column 加上限制,需保证表头在一行展示,部分列的内容要一行展示,自适应单项列的宽度;
Ali-Sentinel-链路控制
message handler
文章浏览阅读4.2k次。环境:vs2019 gmssl 32位编译1、首先新建项目2、在VS的工程设置工程属性(参考连接https://blog.csdn.net/zhonghua_csdn/article/details/99011892)右击工程名 ——> 选择“属性” 在“VC++目录”——> “包含目录”中添加openSSL的include文件(在您安装openssl的文件下) 在“VC++目录”——> “库目录”中添加openSSL的lib文件(在您安装openssl的文件下) 在“._使用c语言调用openssl实现sm4代码
文章浏览阅读73次。来源:http://www.bysjhf.com.cn目前,U盘病毒的情况非常严重,几乎所有带病毒的U盘,根目录里都有一个autorun.inf。右键菜单多了“自动播放”、“Open”、“Browser”等项目。由于我们习惯用双击来打开磁盘,但现在我们双击,通常不是打开U盘,而是让autorun.inf里所设的程序自动播放。所以对于很多人来说相当麻烦。其实Autorun...._linux怎么为windows做autorun免疫
文章浏览阅读1.5k次。Qt Creator 报错:Error while building/deploying project helloworld (kit: Desktop Qt 5.6.2 MinGW 32bit) When executing step "qmake" - zhangjunwu - 博客园 (cnblogs.com)https://www.cnblogs.com/zhangjunwu/p/7417566.html注意:Qt文件路径不要出现中文名字和空格!!!......_error while building/deploying project xianzhazhi (kit: desktop qt 5.12.9 ms
文章浏览阅读1.3k次。Installing packages. This might take a couple of minutes.Installing react, react-dom, and react-scripts with cra-template-typescript...npm ERR! code 1npm ERR! path C:\Users\MHX\Desktop\react-demo\node_modules\canvasnpm ERR! command failednpm ERR! comm_installing packages. this might take a couple of minutes. installing react,
文章浏览阅读1.9w次,点赞43次,收藏214次。关于西电计科本科学习的一些经验分享与资料汇总_西电毕设拿良容易吗
文章浏览阅读279次,点赞9次,收藏3次。项目根目录打开终端,执行以下命令,安装依赖。执行以下命令后,在浏览器中打开。就可以打开这个项目了。
文章浏览阅读8.5k次,点赞4次,收藏24次。关于C++中二维vector使用vector本来就是可以用来代替一维数组的,vector提供了operator[]函数,可以像数组一样的操作,而且还有边界检查,动态改变大小。这里只介绍用它来代替二维的数组,二维以上的可以依此类推。1、定义二维vectorvector<vector<int>> A;//错误的定义方式vector<vector<int> > A;//正缺的定义方式vector<vector<int> > v;/_c++ 二维vector
文章浏览阅读187次。广告关闭腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元!导言:记录下学习的算法题,写练多,脑子才能转的快! 今日算法题:二分法查找说下我对于二分法查找的理解:【和猜数字游戏差不多】 要在一个有序数列中找到一个与对应给定数字。 1、找到有序数列中最中间的数字2、若中间值大于给定值,则在左边数列重新二分查找3、若中间值小于给定值,则在右边数列..._python服务端算法题