当前位置:网站首页>Custom exception classes and exercises
Custom exception classes and exercises
2022-06-28 09:25:00 【User 9857551】
Custom exception classes
Java The provided exception class is not enough , You need to define . Format :
public class XXXException extends Exception / RuntimeException{
Add a null parameter constructor ;
Add a constructor with exception information ;
}Be careful :1. The custom exception class is generally in the form of Exception ending , This class is an exception class . 2. Custom exception classes , Must inherit Exception perhaps RuntimeException Inherit Exception: The custom exception class is a compile time exception , If a compile time exception is thrown inside the method , You have to deal with this exception throws perhaps try/catch Inherit RuntimeException: Abnormal operation period , Can be either , Give to virtual machine .
practice :
Simulated user registration exception , If the user name already exists , Throw an exception , And prompt . analysis : 1. Use the array to save the registered user name ( database ) 2. Use scanner Get the user name entered by the user ( Front page ) 3. Define a method , Judge the user name entered by the user Traverse the stored user name , Get each user name for comparison ture: User name exists : Throw out regiserException abnormal , And said “ Hello , The user name has been registered ”. false: Continue to traverse and compare If the cycle is over , Not yet : congratulations , Registered successfully . Next, define the main method :
Define method judge method :
Call the method in the main method :
Remember to throw exceptions when writing . If you use try/catch Remember to add one return, The ending method , Otherwise, the following code still executes .
边栏推荐
- 小米旗下支付公司被罚 12 万,涉违规开立支付账户等:雷军为法定代表人,产品包括 MIUI 钱包 App
- rman备份报ORA-19809 ORA-19804
- Data visualization makes correlation analysis easier to use
- 怎样在手机上开户?现在网上开户安全么?
- 2022-06-27:给出一个长度为n的01串,现在请你找到两个区间, 使得这两个区间中,1的个数相等,0的个数也相等, 这两个区间可以相交,但是不可以完全重叠
- Common test method used by testers --- orthogonal method
- 线程的生命周期
- English translation plug-in installation of idea
- 全局异常处理器与统一返回结果
- 买卖股票费用计算
猜你喜欢

Linux下安装redis 、Windows下安装redis(超详细图文教程)

全链路业务追踪落地实践方案

Data visualization makes correlation analysis easier to use

JVM系列(2)——垃圾回收

Test cases for learning the basic content of software testing (II)

学习阿里如何进行数据指标体系的治理

Decision table method for basic content learning of software testing (2)

SQL optimization experience: from 30248 seconds to 0.001 seconds

Learn how Alibaba manages the data indicator system

DEJA_VU3D - Cesium功能集 之 051-地形开挖完美实现
随机推荐
Test cases for learning the basic content of software testing (II)
DolphinScheduler使用系统时间
自动转换之-面试题
Campus honey decoration of APP course design (e-commerce platform)
虚拟机14安装win7(图教程)
Stock suspension
为什么SELECT * 会导致查询效率低?
1180: fractional line delimitation /p1068 [noip2009 popularization group] fractional line delimitation
Ingersoll Rand panel maintenance IR Ingersoll Rand microcomputer controller maintenance xe-145m
買賣股票費用計算
PMP考试重点总结五——执行过程组
Screen settings in the source code of OBS Live Room
Implementation of single sign on
Write a simple timeline
Postman interface test
Fastjason filter field
1. Kimball dimension modeling of data warehouse: what is a fact table?
1182:合影效果
JMeter -- interface test 1
Expérience d'optimisation SQL: de 30248 secondes à 0001 secondes