当前位置:网站首页>Chapter 7 exception handling
Chapter 7 exception handling
2022-07-27 12:14:00 【Haha's tea】
Catalog
Two 、 Exception Architecture
3、 ... and 、 Exception handling : Grab and throw the model
Four 、 Exception handling mechanism 1 :try-catch-finally
(2)try-catch-finally in finally Use
5、 ... and 、 The second way of exception handling :throws + Exception types
6、 ... and 、 Throw an exception manually
7、 ... and 、 How to customize exception class ?
8、 ... and 、 summary : exception handling 5 Key words
One 、 Exception Overview

Two 、 Exception Architecture

java.lang.Throwable
(1)java.lang.Error: Generally do not write targeted code for processing
(2)java.lang.Exception: Exception handling is possible
① Compile time exception (checked)
IOException:FileNotFoundException
ClassNotFoundException
② Runtime exception (unchecked)
NullPointerException
ArrayIndexOutOfBoundsException
ClassCastException
NumberFormatException
InputMismatchException
ArithmeticException
Interview questions : What are the common anomalies ? Illustrate with examples
3、 ... and 、 Exception handling : Grab and throw the model
Java Exception handling is provided Grab and throw the model .
Process one :“ throw ”, During the normal execution of the program , Once something unusual happens , An object corresponding to the exception class will be generated at the exception code , And throw this object to Java Runtime system . Once the object is thrown , The subsequent code is no longer executed .
On the generation of abnormal objects :① Exception objects generated automatically by the system
② Manually generate an exception object , And throw (throw)
Process two :“ Catch ”, It can be understood as an exception handling method :①try-catch-finally ②throws
Four 、 Exception handling mechanism 1 :try-catch-finally
(1)try-catch-finally Use
try{
// There may be unexpected code
}catch( Exception types 1 Variable name 1){
// How to handle exceptions 1
}catch( Exception types 2 Variable name 2){
// How to handle exceptions 2
}catch( Exception types 3 Variable name 3){
// How to handle exceptions 3
}
......
finally{
// Code that must execute
}
explain :
1.finally It's optional
2. Use try Wrap up the code that may have an exception , In the process of execution , Once something unusual happens , An object corresponding to the exception class will be generated , According to the type of this object catch Match in
3. once try The exception object in matches to a catch when , To get into catch Exception handling in . Once the process is complete , Just jump out of the current try-catch structure ( Without writing finally Under the circumstances ), Continue with the following code
4.catch If the exception type in does not have a child parent relationship , Who declared on , It doesn't matter who declares that .catch If the exception type in satisfies the child parent relationship , It must be declared on the parent class . otherwise , Report errors
5. Common ways to handle exception objects :①String getMessage() ②printStackTrace()
6. stay try Variables declared in structure , There's something wrong with it try After the structure , Can't be called
7.try-catch-finally Structures can be nested
experience 1: Use try-catch-finally Structure handles compile time exceptions , So that the program will no longer report errors when compiling , But the runtime may still report an error . It's equivalent to we use try-catch-finally Structure will an exception that may occur at compile time , Delay to run time .
experience 2: In development , Because runtime exceptions are common , So it is usually not written for runtime exceptions try-catch-finally It's structured . But for compile time exceptions , Be sure to consider exception handling .
(2)try-catch-finally in finally Use
1.finally It's optional
2.finally What is declared in is code that must be executed . Even if catch There is another exception in ,try There is return structure ,catch There is return Statement, etc .
3. Like database connection 、 I / O stream 、 Network programming Socket And so on ,JVM It can't be recycled automatically , We need to release resources manually . At this time, the resource release needs to be declared in finally in .
5、 ... and 、 The second way of exception handling :throws + Exception types
1."throws + Exception types " Write in the declaration of the method . Indicates when this method is executed , Exceptions that may be thrown .
Once an exception occurs when the method body executes , An object of the exception class is still generated at the exception code , This object satisfies throws After the exception type , Will be thrown . Exception code and subsequent code will not be executed .
2. experience :
try-catch-finally Really handle the exception
throws The only way to do this is to throw the exception to the caller of the method , It didn't really handle the exception .
3. How to choose to use try-catch-finally still throws?
(1) If the overridden method in the parent class does not throws Handle exceptions in different ways , Then subclass overridden methods cannot be used throws, It means that if there is an exception in the method overridden by the subclass , You have to use try-catch-finally Method handling .
(2) Method of execution A in , Several other methods have been called , These methods are implemented by progressive relations . It is suggested that these methods be used throws The way to deal with , And the method of execution A Consider using try-catch-finally Method .
Add ( One of the rules of method rewriting ): The exception thrown by the method overridden by the subclass is no larger than the exception thrown by the method overridden by the parent class
6、 ... and 、 Throw an exception manually
7、 ... and 、 How to customize exception class ?
1. Inherits from the existing exception structure :RuntimeException、Exception
2. Provide global constants :serialVersionUID
3. Provide overloaded constructors
8、 ... and 、 summary : exception handling 5 Key words

边栏推荐
- Binary search decision tree (average search length of binary search tree)
- Unexpected harvest of epic distributed resources, from basic to advanced are full of dry goods, big guys are strong!
- Docker MySQL Usage Note
- 求不同采样周期下的传递函数有限零点
- NewTicker使用
- Detailed explanation of MATLAB S-function
- The first case of monkeypox in pregnant women in the United States: the newborn was injected with immunoglobulin and was safely born
- 【机器学习-白板推导系列】学习笔记---支持向量机和主成分分析法
- Iptables firewall
- Conversion between multiple bases
猜你喜欢

Shell script text three swordsman awk

图像分割 vs Adobephotoshop(PS)

LNMP架构搭建(部署Discuz论坛)

【机器学习-白板推导系列】学习笔记---支持向量机和主成分分析法

哈希表 详细讲解

EfficientNet

TapNet: Multivariate Time Series Classification with Attentional Prototypical Network

Idea: can't use subversion command line client: SVN solution

Lonely young people can't quit jellycat

Introduction to box diagram
随机推荐
Detailed explanation of MATLAB S-function
Keil MDK compilation appears..\user\stm32f10x H (428): error: # 67: expected a "}" wrong solution
mysql分页查询实例_mysql分页查询实例讲解「建议收藏」
【机器学习-白板推导系列】学习笔记---支持向量机和主成分分析法
TapNet: Multivariate Time Series Classification with Attentional Prototypical Network
Unity Shader 一 激光特效Shader[通俗易懂]
Arduino常见供电问题与解决
[excerpt] [medical image] common DICOM thumbnail interpretation and viewer converter conversion tool
npm踩坑
LNMP架构搭建(部署Discuz论坛)
Leetcode 02: sword finger offer 58 - I. flip the word order (simple); T123. Verify palindrome string; T9. Palindromes
deeplab系列详解(简单实用年度总结)
omitempty在go中的使用
基于反馈率的控制系统原理
广东:剧本杀等新行业新业态场所,消防安全监管不再“缺位”
N ¨UWA: Visual Synthesis Pre-training for Neural visUal World creAtionChenfei
Adobe audit prompts that the sampling rate of audio input does not match the output device - problem solving
Example of MATLAB dichotomy (example of finding zero point by dichotomy)
Solution: can not issue executeupdate() or executelargeupdate() for selections
B 站 713 事故后的多活容灾建设|TakinTalks 大咖分享