Exception in Java (1) - basic concept

Happy company 2021-04-08 11:35:33
exception java basic concept


1. JAVA Definition of exception in

abnormal (Exception) : It refers to the error in the running of the program , for example : In Division , If the divisor is 0, Then run JAVA It will automatically throw arithmetic exception .
JAVA abnormal : It is also represented by an object , An exception thrown by a program at run time , It's actually an exception object , This object not only encapsulates the error information , There are also some ways to deal with it .

2. Abnormal system

Abnormal root class :
stay Java in , All anomalies and errors are born of java.lang.Throwable This class . Only if the object is an instance of this class and its subclasses , Can pass Java Virtual machine or Java throw Statement throw . Similarly , Only this class or one of its subclasses can be catch Parameter type in clause .
java.lang.Throwable There are two subclasses :java.lang.Error( error ) And java.lang.Exception( abnormal ), The usual term "abnormal" refers to java.lang.Exception.

Throwable -- Error
|_ Exception -- ClassNotFoundException
|_ IoException -- FileNotFoundException
|_ RuntimeException -- ArithmeticException
|_ ArrayIndexOutOfBoundsException
|_ ClassCastException
|_ NullPointerException
|_ ......

The difference between an exception and a mistake is :
Exceptions can be handled by the program itself , The mistake is that you can't handle .

Throwable yes Java Superclass of all errors or exceptions in a language . Contains a snapshot of the thread execution stack when its thread is created , It provides printStackTrace() And other interfaces are used to obtain stack trace data and other information .
Throwable Commonly used API:

public void printStackTrace() // Print exception details . Contains the type of exception , The cause of the abnormality , It also includes the location of the exception .
public void printStackTrace(PrintStream s): This method is usually used to output the exception content to the log file , In order to see , Similar to redirection .
public String getMessage() // Get the cause of the exception , Return string .
public String toString() // Get exception type and exception description information , Return string .

3. List of common exception classes

Exception class name Meaning of exception class
ArithmeticException Arithmetic exception class
ArraylndexOutOfBoundsException Array subscript out of bounds exception
ArrayStoreException Exception thrown when assigning values incompatible with array types to array elements
ClassCastException Type cast exception
ClassNotFoundException No corresponding exception class found
EOFException End of file exception
FileNotFoundException File found no exception
illegalAccessException Exception thrown when access to a class is denied
IndexOutOfBounchsException An exception is thrown when the index of an object is out of range
InstantiationException Try to pass new Instance() Method to create an instance of an abstract class or interface
IOException I / O is abnormal
NegativeArraySizeException Array negative subscript exception
NoSuchFileException Exception not found for field
NoSuchMethodException Method found no exception
NullPointException Null pointer exception
NumberFormatException String to number exception
OutOfMemoryException The memory allocated to the new object is too small
ProtocolException There is an error in the network protocol
SecurityException Applet An attempt was made to perform an action not allowed by the browser's security settings
StringIndexOutBounchsException The program attempted to access a character position in a string where the character does not exist
SockedException Can't complete normally Socked operation
SQLException Operation database exception

Be careful : If you don't know what kind of exception code throws , You can specify their parent class Throwable and Exception.

4. Exception handling mechanism

  • Throw an exception

  • Capture exception

JVM Default exception handling : Print error messages on the console , And terminate the program

5. Exception handling process :

  • If there is an exception in the program , Then it will be automatically controlled by JVM Depending on the type of exception , Instantiate an object that specifies the exception class ;
  • If there is no exception handling operation in the program at this time , Then the instantiated object of this exception class will be given to JVM To deal with , and JVM The default processing method is to output exception information , Then interrupt the program execution
  • If there is exception handling in the program , It will be try Statement to capture the resulting exception class object
  • And try Every one after catch Match , If the match is successful , Then use the specified catch To deal with , If no match succeeds , And then to the back catch Continue matching , If there is nothing catch The match is successful , At this time, it will be handed over to JVM Perform default processing
  • It will be executed whether there is an exception or not finally Program , If there is no exception at this time , After execution finally, Will continue to execute other code in the program , If there is an exception at this time, it can not be handled ( None of them catch You can meet ), Then it will be carried out as well finally, But after the execution finally after , Give the default to JVM Output abnormal information , And the program interrupts

5. Exception handling in development :

  • try....catch(finally): Capture , Do it yourself

  • throws: Throw out , Leave it to the caller

  • Custom exception classes


author : Happy company

https://www.cnblogs.com/jddreams/p/14281900.html

版权声明
本文为[Happy company]所创,转载请带上原文链接,感谢
https://javamana.com/2021/04/20210408110426063G.html

  1. Introduction and practice of springboot cache
  2. 解决No enum constant org.apache.ibatis.type.JdbcType.XXX
  3. Explain HTTP message in detail
  4. Solve no enum constant org.apache.ibatis . type.JdbcType.XXX
  5. It is not only redis that can do caching
  6. JVM bytecode instruction manual - view Java bytecode
  7. How much do you know about Java data types?
  8. ELF_ Plead -- blacktech hackers organize malware for Linux
  9. Introduction to HTTP message format
  10. MySQL 数据类型
  11. MySQL基础命令中文解析
  12. JDBC事务
  13. JDBC transaction
  14. 1.3Java基础入门【运算符】
  15. Spring cloud course: 12. Similarities and differences of Eureka / consult / zookeeper
  16. Linux operation and maintenance (1) - command line
  17. Java number class, character class, string class
  18. Java计算机IT编程文档常见单词翻译
  19. SpringBoot通过AOP实现系统日志记录(一)-Controller层日志监控(包括系统日志监控)
  20. Tencent interview: how to achieve the isolation level between MySQL transaction and mvcc?
  21. Translation of common words in it programming documents of Java computer
  22. SpringBoot通过AOP实现系统日志记录(一)-Controller层日志监控(包括系统日志监控)
  23. Algorithm - a classic SQL question and a Java algorithm question
  24. Algorithm - a classic SQL question and a Java algorithm question
  25. Tencent interview: how to achieve the isolation level between MySQL transaction and mvcc?
  26. Oracle MySQL programming self study and interview guide 06-04: string type
  27. Principle analysis of MySQL mvcc
  28. Zookeeper使用场景
  29. Usage scenarios of zookeeper
  30. JAVA_逻辑运算符与位运算符使用
  31. MySQL date and time function summary
  32. Simple use of Java date class
  33. Basic learning of JavaScript (1)
  34. Hive compression and column storage
  35. Constants, variables and data types in Java
  36. Detailed explanation of java basic data type
  37. Front end interview of HTTP protocol things!
  38. Oracle Data Types(数据类型)
  39. Dubbo、SpringCloud
  40. Geohash principle and operation of redis Geo
  41. Oracle data types
  42. MySQL data archiving tool Recommendation -- MySQL_ archiver
  43. Springboot mini - Solon详解(七)- Solon Ioc 的注解对比Spring及JSR330
  44. Spring boot mini - Solon (7) - annotation comparison of Solon IOC spring and jsr330
  45. Linux操作系统的常用命令
  46. Common commands of Linux operating system
  47. Java 基本类型
  48. Java basic types
  49. MySQL学习-排序与分组函数
  50. MySQL性能与调优
  51. MySQL learning sorting and grouping function
  52. MySQL performance and tuning
  53. java object
  54. Shortcut keys for eclipse
  55. Summary of Java Architect's growth path (continuous update)
  56. Introduce an OA open source product based on springboot!
  57. JavaScript 日期时间格式化
  58. JavaScript date time formatting
  59. Linux common command collection, memo, Notepad, online record
  60. MYSQL