当前位置:网站首页>Exception handling mechanism II
Exception handling mechanism II
2022-07-26 09:19:00 【Fat and often sad】
Declaration throws an exception
Declaration throw exception is Java The second way to handle exceptions in
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 to deal with it Species abnormality , 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 The following exception types can be So is the type of exception generated in the method , It can also be its parent class .
Declare an example of throwing an exception :


Override the principle of method declaration throwing exceptions
An overriding method cannot throw an exception type that is larger than the scope of the overridden method . In the case of polymorphism , Yes methodA() Method call - Exception capture is handled according to the exception declared by the parent class .
Throw an exception manually
Java Exception class objects are automatically generated by the system and Throw out , It can also be manually created and thrown as needed .
1、 First of all, we need to generate exception class objects , And then through throw Statement to implement the throw operation ( Submit to Java shipment Business environment ). IOException e = new IOException();
throw e;
2、 The exception that can be thrown must be Throwable Or an instance of its subclass . The following statement will There will be grammatical errors :
throw new String(“want to throw”)
User defined exception class
1、 In a general way , User defined exception classes are RuntimeException Subclasses of
2、 Custom exception classes usually need to write several overloaded constructors .
3、 Custom exceptions need to provide serialVersionUID
4、 Custom exception through throw Throw out .
5、 The most important thing about custom exceptions is the name of the exception class , When an exception occurs , According to Name determines the exception type .
User defined exception class MyException, It is used to describe the error information of data value range . user Your own exception class must inherit the existing exception class .
summary : exception handling 5 Key words 
边栏推荐
猜你喜欢
随机推荐
When you click input, the border is not displayed!
Windows通过命令备份数据库到本地
Flask project learning (I) -- sayhello
Qtcreator reports an error: you need to set an executable in the custom run configuration
省政府召开全省高温天气安全防范工作电视电话会议
Grain College of all learning source code
Error: Cannot find module ‘umi‘ 问题处理
Thread Join 和Object wait 的区别
volatile 靠的是MESI协议解决可见性问题?(下)
2022茶艺师(中级)特种作业证考试题库模拟考试平台操作
The essence of attack and defense strategy behind the noun of network security
CF1481C Fence Painting
MySQL 强化知识点
Paper notes: knowledge map kgat (unfinished temporary storage)
2022 tea artist (intermediate) special operation certificate examination question bank simulated examination platform operation
LeetCode三数之和问题
ONTAP 9文件系统的限制
Innovus卡住,提示X Error:
NTT (fast number theory transformation) polynomial inverse 1500 word analysis
C# Serialport的发送和接收









