当前位置:网站首页>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
边栏推荐
猜你喜欢
垂直搜索
Object 的Wait Notify NotifyAll 源码解析
【线上死锁分析】由index_merge引发的死锁事件
2022 tea artist (intermediate) special operation certificate examination question bank simulated examination platform operation
2022茶艺师(中级)特种作业证考试题库模拟考试平台操作
Cat安装和使用
Redis principle and usage - installation and distributed configuration
Innovus is stuck, prompting x error:
Elastic APM installation and use
2022年上海市安全员C证考试试题及模拟考试
随机推荐
李沐d2l(五)---多层感知机
JVM command induction
Under a directory of ext3 file system, subfolders cannot be created, but files can be created
“No input file specified “问题的处理
垂直搜索
Object type collections are de duplicated according to the value of an attribute
十大蓝筹NFT近半年数据横向对比
MySQL 强化知识点
2B and 2C
The essence of attack and defense strategy behind the noun of network security
volatile 靠的是MESI协议解决可见性问题?(下)
JVM触发minor gc的条件
Unity topdown character movement control
力扣刷题,三数之和
2022 Shanghai safety officer C certificate examination questions and mock examination
【Flutter -- 布局】Align、Center、Padding 使用详解
Order based evaluation index (especially for recommendation system and multi label learning)
(2006,Mysql Server has gone away)问题处理
巴比特 | 元宇宙每日必读:元宇宙的未来是属于大型科技公司,还是属于分散的Web3世界?...
Babbitt | metauniverse daily must read: does the future of metauniverse belong to large technology companies or to the decentralized Web3 world