(JavaSE)认识异常
2023-09-27 14:28:30 时间
⭐异常的分类
⭐异常的处理
先操作, 遇到问题再处理。后面代码可以执行
🎈try-catch捕获再处理
public static void main(String[] args) {
try{
System.out.println(10/0);
}catch(ArithmeticException e){
System.out.println("算数异常!");
}
System.out.println("执行后续代码!");
}
算数异常!
执行后续代码!
如果没有去捕捉相应的异常,异常就会交给JVM处理,只要交给JVM处理程序就会立即终止。
异常都是一个类
⭐异常的抛出
上述异常都是在程序运行/编译时抛出的,我们也可以主动抛出异常
🎈主动抛异常
🔥抛非受查异常
🔥抛受查异常
抛父类Exception默认是受查异常
public static void test22(int a) throws CloneNotSupportedException,ArithmeticException,NullPointerException {
可以声明受查或非受查异常,可以声明多个
⭐Finally
提示程序可以自动关闭scanner
try (Scanner scanner = new Scanner(System.in)) {//程序会自动关闭scanner
int n = scanner.nextInt();
System.out.println(n);
} catch (InputMismatchException e) {
e.printStackTrace();
System.out.println("输入的数据不匹配");
} finally {
System.out.println("执行了finally部分,一般用来关闭资源!");
}
finally部分一定会被执行
import java.util.InputMismatchException;
import java.util.Scanner;
public class TestFinally {
public static int getData() {
Scanner sc = null;
try {
sc = new Scanner(System.in);
int data = sc.nextInt();
return data;
} catch (InputMismatchException e) {
e.printStackTrace();
} finally {
return 100;
}
}
public static void main(String[] args) {
int data = getData();
System.out.println(data);
}
}
10
100
⭐自定义异常类
🎈自定义运行时异常
//LogIn.java
public class LogIn {
private String userName = "admin";
private String password = "123456";
public void loginInfo(String userName, String password) {
try{
if (!this.userName.equals(userName)) {
throw new UserNameErrorException("用户名错误!");
}
if (!this.password.equals(password)) {
throw new PassWordException("用户名错误!");
}
System.out.println("登陆成功");
}catch(UserNameErrorException e){
e.printStackTrace();
}catch(PassWordException e){
e.printStackTrace();
}
}
public static void main(String[] args) {
LogIn logIn=new LogIn();
logIn.loginInfo("admin123", "123456");
}
}
//UserNameErrorException.java
public class UserNameErrorException extends RuntimeException{
public UserNameErrorException(){
super();
}
public UserNameErrorException(String message){
super(message);
}
}
//PassWordException.java
public class PassWordException extends RuntimeException{
public PassWordException(){
super();
}
public PassWordException(String message){
super(message);
}
}
输出
UserNameErrorException: 用户名错误!
at LogIn.loginInfo(LogIn.java:7)
at LogIn.main(LogIn.java:26)
Process finished with exit code 0
🎈自定义编译时异常
public class UserNameErrorException extends Exception{
public class PassWordException extends Exception{
继承Exception类可以自定义编译时异常
处理办法:
public class LogIn {
private String userName = "admin";
private String password = "123456";
public void loginInfo(String userName, String password) throws UserNameErrorException, PassWordException {
if (!this.userName.equals(userName)) {
throw new UserNameErrorException("用户名错误!");
}
if (!this.password.equals(password)) {
throw new PassWordException("用户名错误!");
}
System.out.println("登陆成功");
}
public static void main(String[] args) {
try{
LogIn logIn=new LogIn();
logIn.loginInfo("admin123", "123456");
}catch(UserNameErrorException e){
e.printStackTrace();
}catch(PassWordException e){
e.printStackTrace();
}
}
}
相关文章
- 【多线程】线程池吃掉异常 submit
- Spring Boot and Rabbit MQ 异常的时候消息的状态
- Java异常处理的误区和经验总结
- ASP.NET MVC开发中常见异常及解决方案
- 10点睛Spring MVC4.1-全局异常处理
- java-proxool 异常
- RSA加密/解密 Decryption error异常解决
- Tomcat中部署后JspFactory报异常的解决方案
- Laravel ->Exceptions ->Handler-> render错误异常的封装
- 【华为OD机试真题 Java】异常的打卡记录 (A卷2022Q4)
- 【JavaSE】Java中的异常那些事儿
- JavaSE——异常处理机制
- mysql遇见contains nonaggregated column 'information_schema.PROFILING.SEQ'异常