当前位置:网站首页>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

边栏推荐
- go 用本地代码replace
- [machine learning whiteboard derivation series] learning notes --- conditional random fields
- Do you really understand the underlying data structure skip list of Zset in redis?
- go入门篇 (4)
- Shell script text three swordsman awk
- 微信小程序必用接口「建议收藏」
- Can you really write binary search - variant binary search
- Finding the finite zero point of transfer function under different sampling periods
- TapNet: Multivariate Time Series Classification with Attentional Prototypical Network
- Guangdong: fire safety supervision is no longer "absent" in new industries and new formats such as script killing
猜你喜欢

Proteus8专业版破解后用数码管闪退的解决

Why is ack=seq+1 when TCP shakes hands three times

Guangdong: fire safety supervision is no longer "absent" in new industries and new formats such as script killing

【机器学习-白板推导系列】学习笔记---概率图模型和指数族分布

NPM step pit

Shell脚本文本三剑客之awk

JS-寄生组合式继承

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

MySQL数据库主从复制集群原理概念以及搭建流程

我在英国TikTok做直播电商
随机推荐
After Party A's hard work, 49.08 million orders of China Mobile were scrapped
kazoo使用教程
Could not load dynamic library ‘libcudnn.so.8‘;
[网摘][医学影像] 常用的DICOM缩略图解释以及Viewer converter 转换工具
希腊字母读法
MySQL paging query instance_ MySQL paging query example explanation "suggestions collection"
Vscode removes style / syntax highlighting / code highlighting / black background when copying code
Guangdong: fire safety supervision is no longer "absent" in new industries and new formats such as script killing
Weibo comment crawler + visualization
matlab二分法例题(用二分法求零点例题)
【机器学习-白板推导系列】学习笔记---条件随机场
EfficientNet
Wilcoxon rank sum and signed rank
shell编程之免交互
Detailed explanation of MATLAB S-function
Some commonly used shortcut keys for MathType
Sword finger offer notes: t57 - I. and two numbers of S
go语言之sync.Map
基于反馈率的控制系统原理
deeplab系列详解(简单实用年度总结)