当前位置:网站首页>Concept and handling of exceptions
Concept and handling of exceptions
2022-07-26 13:27:00 【A stream kite CD】
1、 Concept
An exception is an instruction flow that causes a program to interrupt .
Thowable Class is Java Superclass of all errors or exceptions in a language .
2、Throwable The classification of
Error:
error , deadly , Unrecoverable errors , Programmers are hard to deal with
Exception:
abnormal , Can handle , use Exception handling mechanism To deal with 
There are two ways to deal with it : Display processing (try{}catch(Exception){})
Do not display processing (throw,throws)
3、 Exception handling mechanism
try{}catch(){}finally{}
Be careful :
- Exception handling can only be from top to bottom, from fine to coarse , Not from thick to thin
- try catch finally The three cannot exist separately , But the three can coexist (try And catch,try And finally Can coexist )
- finally: Is the only exit as an exception handling mechanism
4、 Some common exceptions
- Arithmetical abnormality ( Divisor is 0):java.lang.ArithmeticException
- Number format conversion exception :
java.lang.NumberFoematException - Array out of bounds exception :
java.lang.ArrayIndexOutOfBoundsException - Null pointer exception :java.lang.NullPointerException
- Class conversion exception :java.lang.ClassCastException
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
try {
System.out.println(" Please enter the first number ");
String a = sc.next();
int num1 = Integer.parseInt(a);
System.out.println(" Please enter the second number ");
String b = sc.next();
int num2 = Integer.parseInt(b);
int c = num1 / num2;
System.out.println(" The result is :" + c);
Object obj=null;
System.out.println(obj.hashCode());
args=new String[2];
System.out.println(args[3]);
}catch(ArithmeticException e){
System.out.println(" Arithmetical abnormality ");// if b The input is 0, There is an arithmetic exception
}catch(NumberFormatException e){
e.printStackTrace();
System.out.println(" Number format conversion exception ");// If the input is a non numeric character , Number format conversion exception occurs
}catch(ArrayIndexOutOfBoundsException e) {
// The length printed exceeds the length of the array, and an array out of bounds exception occurs
e.printStackTrace();
}catch(NullPointerException e){
// If no object is specified, a null pointer exception will occur
e.printStackTrace();
System.out.println(" Null pointer exception ");
}catch(Exception e){
e.printStackTrace();
}finally{
System.out.println(" No matter whether the program is abnormal or not , Always carry out .");
}
System.out.println(" end ");
}
}
边栏推荐
- Analysis on the current situation and optimization strategy of customer experience management in banking industry
- Probability theory and mathematical statistics
- How to face scientific and technological unemployment?
- Sword finger offer (x): rectangular coverage
- File upload and download performance test based on the locust framework
- Can I take your subdomain? Exploring Same-Site Attacks in the Modern Web
- PostgreSQL official website download error
- Brief introduction of reflection mechanism
- B+ tree selection index (2) -- MySQL from entry to proficiency (23)
- Kubernetes apiserver current limiting strategy
猜你喜欢

PostgreSQL official website download error

概率论与数理统计

JSON数据传递参数&日期型参数传递

飞盘,2022年“黑红”顶流

Detailed relation extraction model casrel

估值15亿美元的独角兽被爆裁员,又一赛道遇冷?

【Oauth2】五、OAuth2LoginAuthenticationFilter

官宣!艾德韦宣集团与百度希壤达成深度共创合作

Probability theory and mathematical statistics
Control the probability of random winning [C | random]
随机推荐
Ultimate doll 2.0 | cloud native delivery package
Sword finger offer (x): rectangular coverage
Kubernetes apiserver current limiting strategy
JSON格式执行计划(6)—mysql执行计划(五十二)
目标检测网络R-CNN 系列
The use of asynchronous thread pool in development
With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
[applet] why can't the onreachbottom event be triggered? (one second)
天津市应急局与驻津央企签署协议深化应急联动机制建设
B+ tree (4) joint index -- MySQL from entry to proficiency (16)
Dimension disaster dimension disaster suspense
Learn about Pinia state getters actions plugins
Oom caused by improper use of multithreading
Sword finger offer (IX): abnormal jumping steps
The serialization class in unity is in JSON format
飞盘,2022年“黑红”顶流
Sword finger offer (21): push in and pop-up sequence of stack
MVVM architecture encapsulation of kotlin series (kotlin+mvvm)
B+树(4)联合索引 --mysql从入门到精通(十六)
12 brand management of commodity system in gulimall background management