Java topic notes~ about errors and exceptions

 

 

Ø Unchecked exception (unckecked exception): Error and RuntimeException and their subclasses. When javac compiles, it will not prompt and find such exceptions, and does not require programmers to deal with these exceptions. In the running phase, if an Error occurs, the virtual machine will almost crash. If a RuntimeException occurs, if the programmer does not handle it, it will be thrown back to the java virtual machine for processing. Of course, if programmers want to, they can also write code to handle (using try...catch...finally) such exceptions (but usually they don't do this. The situation that needs to be done is, for example, in this professional field of mathematical operations. ArithmeticException). For these exceptions, we should fix the code instead of handling them through exception handlers. The reason for this kind of exception is mostly that there is a problem with the code writing. Such as dividing by 0 error ArithmeticException, wrong cast error ClassCastException, array index out of bounds ArrayIndexOutOfBoundsException, using an empty object NullPointerException and so on.

Ø Checked exception (checked exception): Other exceptions except Error and RuntimeException. javac forces programmers to prepare for such exceptions (using try...catch...finally or throws). In the method, either use the try-catch statement to capture it and process it, or use the throws clause statement to throw it, otherwise the compilation will not pass. Such exceptions are generally caused by the operating environment of the program. Because the program may be run in various unknown environments, and the programmer cannot intervene in how the user uses the program he wrote, the programmer should always be prepared for such an exception. Such as SQLException, IOException, ClassNotFoundException and so on.

Q&A: Why add a transaction manager?

Answer: Because different technical management affairs have different categories, such as:

JDBC:Connecton con.commit(); con.rollback();

MyBatis:SqlSession sqlSession.commit(); sqlSession.rollback();

Hibernate:Session session.commit(); session.rollback();

The transaction manager is used to generate the connection object of the corresponding technology and execute the statement.

Guess you like

Origin blog.csdn.net/qq_53142796/article/details/132172970