当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
分布式限流 redission RRateLimiter 的使用及原理
The battle-hardened programmer was also deceived by a fake programmer from a certain fish. The trust between programmers should be the highest, and he alone destroyed this sense of trust
VLAN实验
C language - bitwise operations
时间序列曲线相似性
EA中的业务对象和业务实体你分得清吗?
Static LED display developed by single chip microcomputer
MySQL——数据库基础
The package of idea is not hollow
Native js create table
重写并自定义依赖的原生的Bean方法
Assembly to implement bubble sort
UE5 GAS Study Notes Postscript 0
基于空间特征选择的水下目标检测方法
API 网关 APISIX 在Google Cloud T2A 和 T2D 的性能测试
【云筑共创】华为云携手鸿蒙,端云协同,培养创新型开发者
ADC0808/9 signal acquisition developed by single chip microcomputer
Database transactions, JDBC operations and data types
async.js入门
[Database basics] redis usage summary













