当前位置:网站首页>2022-07-29 Gu Yujia Study Notes Exception Handling
2022-07-29 Gu Yujia Study Notes Exception Handling
2022-07-30 11:37:00 【haha sister】
异常处理
- 下标越界
- 空指针
- 类型转换异常
- 数字格式化
- 算术异常(数学异常)
The three biggest anomalies in the programming world
1.除数为0
2.IO流,没有关闭
3.停电
When an exception occurs in a program,The statement after the exception is thrown is no longer executed,类似于return的功能,终止方法的执行.
异常的体系结构
最顶级的Throwable:错误,异常
Error:正常情况下,不太可能出现,绝大多数Errorwill cause the program to be in an abnormal state, Basically hard to recover.在外力的作用下,不考虑.Error是Throwable的子类,它是在Java outside the scope of program processing
Exception:在Java语言中,An abnormal situation that occurs during program execution is called an exception.Exception也是 Throwable的子类.
编译期异常:写代码的时候,抛异常.If the compiler doesn't resolve it,会编译不通过
运行期异常:RuntimeException,运行时会抛异常,平时没事
自定义异常:
JavaAlthough the exception mechanism is perfect,But it is still far from the actual business.
Negative age requires a custom exception、E-commerce search garbled characters require custom exceptions
怎么自定义异常?
- All exceptions must beThrowable的子类(大材小用,没必要)
- If you want to define a compile-time exception,需要继承Exception类
- If you want to define a runtime exception,需要继承RuntimeException类
在一个语句块中,如果使用throwThrows a compile-time exception,It must be used in the declaration of the methodthrowskeyword to mark the exception type.
还有一种处理方式,直接try ... catch
问题
Why do we have to manually throw exceptions?
Because it is necessary to cooperate with the global exception handling mechanism to solve the problem
throwA statement can be used as the return value of a method
在一个有返回值的方法中,If there is a conditional branch,Be sure to have a return value in every case,Even if it throws an exception.
开发中,Runtime exceptions are used in most cases
异常链
After an exception is thrown, it will continue to be caught or thrown by the method that called the method,Anomalies will spread
Just say resolve the exception、处理异常、Catching exceptions is just thattry ... catch
throws
If a method does not catch a compile-time exception,该方法必须使用throws来声明
(1)在方法里直接try ... catch解决
(2)Throw an exception in the method to the main function,在主函数里try ... catch解决
规定:It is not allowed to continue throwing exceptions in the main method
throwsdoesn't really resolve the exception,Just throw the exception to the next level caller.
面试题
面试题:throws和throw的区别
throwsAppears in the declaration of a method,可以抛出多个异常(用逗号隔开)
finally关键字:
finally用来创建在try代码块后面执行的代码块
无论是否发生异常,finally代码块中的代码一定会执行.一班finallyThe code in is used to release resources.
Finally无论是否出现异常,都会执行
面试题01:try ... catch、finally执行顺序
finallyis always executed last
如果在try ... catch语句中有return,
catchMultiple exceptions can be written(可以捕获多个异常)
顺序问题:Write small first,write big
Another rule to rewrite:The overridden method cannot throw a larger exception type than the overridden method
边栏推荐
猜你喜欢
随机推荐
明德扬FPGA开发板XILINX-K7核心板Kintex7 XC7K325 410T工业级
pg_rewind 修复主备环境的时间线
物联网技术概论:第6章
Detailed explanation of @RequestBody and @ResponseBody
模糊离散事件系统的可测性
Static LED display developed by single chip microcomputer
基于MySQL数据库,Redis缓存,MQ消息中间件,ES搜索引擎的高可用方案解析
Transfer Learning技术研修
向上管理读书笔记
Voltage relay HDY - vac - 1 A / 1-220
unity3d C#语言基础(继承)
JSP 语法简介说明
ADC0808/9 signal acquisition developed by single chip microcomputer
The package of idea is not hollow
《跟唐老师学习云网络》 - 问题定位 - 主机通但容器不通
ODrive应用 #4 配置参数&指令「建议收藏」
spin lock和mutex使用场景的差异
MySQL——数据库基础
Jingdong school recruited written test questions + summary of knowledge points
Vim plugin GrepIt