当前位置:网站首页>Exception -exception
Exception -exception
2022-07-27 21:41:00 【Xiao Tang learns to catch babies】
== = = = = = = = abnormal - Exception = = = = = = = ==
One 、 Basic concepts
Java In language , The abnormal situation in program execution is called “ abnormal ”.( Syntax and logic errors in the development process are not exceptions )
Abnormal events during execution can be divided into two categories
1)Error( error ):Java Serious problems that virtual machines can't solve .
Such as :JVM System internal error 、 Serious situations such as resource exhaustion .
such as :StackOverflowError[ Stack overflow ] and OOM(out of memory).
Error It's a serious mistake , The program will crash .
2) Exception: Other general problems caused by programming errors or accidental external factors , You can use targeted code for processing .
For example, null pointer access , Trying to read a file that doesn't exist , Network connection interruption, etc ,Exception There are two main categories :
Runtime exception [ Program runtime , Abnormal occurrence ] and
Compile time exception [ When programming , Exception detected by compiler ].
Two 、 Exception system diagram 
Summary of exception system diagram
1. Exceptions fall into two categories , Runtime and compile time exceptions .
2 Runtime exception , The compiler can't check . It generally refers to the logic error in programming , Is the exception that programmers should avoid .java.lang.RuntimeException Class and its subclasses are runtime exceptions
3. For runtime exceptions , You don't have to deal with it , Because this kind of anomaly is very common , If the whole process may affect the readability and efficiency of the program
4. Compile time exception , Is an exception that the compiler requires to handle .
3、 ... and 、 Common runtime exceptions
- NullPointerException Null pointer exception
- ArithmeticException The mathematical operation is abnormal
- ArrayIndexOutOfBoundsException Array subscript out of bounds exception
- ClassCastException Type conversion exception
- NumberFormatException Incorrect number format, exception []
3、 ... and 、 Common compilation exceptions
Compilation exception means that during compilation , An exception that must be handled , Otherwise, the code cannot be compiled
Common compilation exceptions are :
1、SQLException// When operating the database , An exception may occur in the query table
2、IOException// When operating files , Abnormal occurrence
3、FileNotFoundException T// When operating on a nonexistent file , Something goes wrong
4、ClassNotFoundException // Load class , When this class does not exist , abnormal
5、EOFException// Operation file , To the end of the file , Something goes wrong
6、lllegalArguementException // Parameter exception
Four 、 exception handling
Exception handling is when an exception occurs , How to handle exceptions .
How to handle exceptions :
1) try-catch-finally
The programmer catches the exception in the code , Do it yourself
2) throws
Throw the exception that occurs , Give it to the caller ( Method ) To deal with it , The top processor is JVM
① try-catch-finally Processing mechanism
try {
The code may have exceptions
}catch(Exception e) {
// Exception caught
//1. When an exception occurs
2. The system encapsulates exceptions into Exception object e, Pass to catch
//3 After getting the exception object , The programmer , Do it yourself
//4. Be careful , If nothing unusual happens
catch Code blocks do not execute
}finally{
//1. No matter try Whether there are exceptions in the code block , Always carry out finally
//2. therefore , Code that will usually release resources , Put it in finally
}
matters needing attention :
1) If something goes wrong , The code behind the exception will not execute , Direct access to catch block .
2) If the exception does not occur , Then execute in sequence try Code block for , Will not enter into catch.
3) If you want to, whether or not an exception occurs , All execute a piece of code ( For example, close the connection , Release resources, etc )
Then use the code - finally
②throws Processing mechanism 
1) If there's a way ( When the statement in is executed ) It may generate some kind of exception , But I'm not sure how
Handle this exception , This method should explicitly declare that an exception is thrown , Indicates that this method will not handle these exceptions , It's up to the caller of the method to handle .
2) Use... In the method declaration throws Statement can declare a list of exceptions thrown ,throws Difference behind
A constant type can be an exception type generated in a method , It can also be its parent class .
matters needing attention
1) For compilation exceptions , Must be handled in the program , such as try-catch perhaps throws
2) For runtime exceptions , If there is no processing in the program , The default is throws How to deal with [ give an example ]
3) When a subclass overrides a method of a parent class , Rules for throwing exceptions : Subclass override method , The type of exception thrown is either the same as the exception thrown by the parent class , Or the subtype of the type of exception thrown for the parent class [ give an example ]
4) stay throws In the process , If there is a way try-catch, It's equivalent to handling exceptions , You don't have to
throws
5、 ... and 、 Custom exception
1、 Basic concepts
When something appears in the program “ error ”, But the error message is not in Throwable Subclass describes the handling of , You can design your own exception class at this time , Used to describe the error message .
2、 To customize an exception
1) Defining classes : Custom exception class name ( The programmer writes it himself ) Inherit Exception or RuntimeException
2) If you inherit Exception, This is a compilation exception
3) If you inherit RuntimeException, It belongs to operation exception ( Generally speaking , Inherit RuntimeException)
6、 ... and 、throw and throws The difference between

边栏推荐
- ACM mm 2022 | Zhejiang University proposed: point cloud segmentation, active learning of new SOTA
- Worthington phospholipase A2 study phosphatidylcholine 2-acetylhydrolase
- Technical practice behind bloom model: how to refine 176billion parameter model?
- Troubleshooting and resolution of program operation problems: an instance of 'std:: Logic_ error‘what(): basic_ string::_ M_ construct null not valid
- Puzzle (002) inner solid, outer solid, Hamilton
- ZABBIX monitoring service (III) configuration management graphics and windows
- 疫情之下,手机供应链及线下渠道受阻!销量骤降库存严重!
- Some operations about Anaconda (installing software and quickly opening)
- Principle analysis and best practice of guava cache
- @Component可以和@Bean 用在同一个类上吗?
猜你喜欢

IDEA常用快捷键及设置方法

30 minutes to thoroughly understand the synchronized lock upgrade process

Mobilevit learning notes

Small change project (two versions) with detailed ideas

PostgreSQL source code (65) analysis of the working principle of globalvis, a new snapshot system

Idea connects to MySQL database and performs SQL query operations
Excalidraw: an easy-to-use online, free "hand drawn" virtual whiteboard tool

@RequestParam注解的详细介绍

@Detailed introduction of requestparam annotation

哈希表的查找与插入及删除
随机推荐
University of Tilburg, Federal University of the Netherlands | neural data to text generation based on small datasets: comparing the added value of two semi supervised learning approvals on top of a l
2019q4 memory manufacturers' revenue ranking: Samsung fell 5%, only SK Hynix and micron maintained growth
For 3nm and below processes, ASML new generation EUV lithography machine exposure
[day_4-review, basic concepts of objects and arrays - 1]
After sorting (bubble sorting), learn to continuously update other sorting methods
Search, insert and delete of hash table
In addition to "adding machines", in fact, your micro service can be optimized like this
Yyds dry inventory learn how to write function overloads in typescript
为什么服务端程序都需要先 listen 一下
对L1正则化和L2正则化的理解[通俗易懂]
聊聊 MySQL 事务二阶段提交
How to check whether there is Tsinghua source / delete Tsinghua source and keep the default source
除了「加机器」,其实你的微服务还能这样优化
疫情之下,手机供应链及线下渠道受阻!销量骤降库存严重!
STL源码剖析
Characteristics and determination scheme of Worthington mushroom polyphenol oxidase
Graphic SQL, this is too vivid!
Puzzle (021) eliminate problems
声扬科技正式上线闻声远程声纹健康回访服务系统!
Ziguang zhanrui: dozens of 5g terminals based on chunteng 510 will be commercially available in 2020