Java Handling Exception Mechanism

exception handling


1. Disadvantages
of
not using
the exception mechanism

2. Exception mechanism
01. Exception An abnormal
event that occurs during the running of a program will interrupt the normal running program.
02. The Java programming language uses the exception handling mechanism to provide the program with the ability to handle errors
. Exception handling steps in the program:
001. The program is pre-set to deal with the exception.
002. The program runs
003. Exception
004. Handle exceptions
005. After processing, the program continues to run

3. Java's exception handling is realized through 5 keywords
01.try
executes code that may generate exceptions
02.catch
catches exceptions
03.finally code can always
execute regardless of whether an exception
occurs Declare the various exceptions that the method may throw Syntax: try{ //code segment }catch(Exception type (Exception) ex){






//The code segment that handles the exception
}fanilly{
//Code segment
}
*Exception is a special object of type java.lang.Exception or its subclasses

4.编译器的异常抛出
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at Demo01.main(Demo01.java:7)

01.Exception in thread "main" java.util.InputMismatchException
*The type of exception thrown is
02.at Demo01.main(Demo01.java:7) 001.main: Exception 002.(Demo01
was thrown in this method
.java:7): where the exception was thrown

5. Commonly used methods in exception classes
01.printStackTrace() throws exception
memory stack information and error location
*outputs exception stack information ) part of the output


6. Common exception types
01. The parent class of the Exception
hierarchy
02. ArithmeticException arithmetic error situations
, such as dividing by 0 Class 06.IllegalArgumentException method received an illegal parameter











7. Inherited relationships in
Java 01.Object (the top-level parent class of the exception class)
001.Error (errors that cannot be handled by the program (non-checked exceptions))
002.Exception (exceptions that the program can handle)
0001. Non-query exceptions
00001. RuntimeException
00002.ArrayIndexOutOfBoundsException
00003.ArithmeticException
00004.NumberFormatException
00005.NullPointerException
0002.Non-RuntimeException (Checked Exception)
00001.IOException
00002.EoFException
00003.FileNotFoundException
00004.SQLException
00005.ClassNotFoundException

8. Generate exception statement
01. Shortcut method
001. Select code
002. Alt + Shift + Z
02. Manual
001. Select code
002. Right click → surround With → try(try catch block)

9. The program exits
01.System.exit()
001. Method with parameters If
the parameter is 0, the program exits normally . If the
parameter is not 0, the program is forced to exit (close the Java virtual machine)

10. Summary
01. There is a return statement in the try-catch block, whether to execute the finally block, and if so, the execution order
Execution order:
try ---> catch ---> finally ---> return
02.try-catch-finally In the block, the only case where the finally block is not executed
Use the System.exit() method, because the Java virtual machine will be shut down

11. Summary
01. Exceptions are divided into Checked exceptions and runtime exceptions
001. Checked exceptions must be caught or declared to be thrown
002. Runtime exceptions are not required to be caught or
famously thrown Sequence
try ---> catch ---> finally ---> return
03. The statement in the finally block is not executed The
System.exit() statement appears!

-------------------------------------------------- ------------------------------------
Exception log

1. throws (the system automatically throws exceptions)
01. Syntax
access modifier return value type method name () throws Exception{

}
02. Declare an exception and throw it to the caller.
*The caller must use try-catch for processing
03. Position
The exception is declared after the method name, and multiple exceptions are separated by commas

2. Throw (manually throw exceptions)
01. Syntax:
throw new Exception (throw content!! );
02. Manually throw exception handling
001. Use try-catch statement to handle
002. Use throws to throw to the caller, let the caller use try-catch statement to handle the exception

3. Exception classification
01.Throwable
001.Error
Serious errors that cannot be recovered by the program itself,
such as:
0001.AWTError
0002.ThreadDeath
..........
002.Exception
is thrown and handled by the Java application Non-serious error
0001.SQLException
0002.ClassNotFoundException
......
*Checked exception, the program must handle this type of exception
------------------------- ------------
0001.RuntimeException (runtime exception, program does not require processing)
00001.ArithmeticException (runtime exception)
00002.NullPointerException (null pointer exception)
00003.NumberFormatException (format conversion exception)
............

4. The difference between throws and throws
01.throw
001. Only one exception can be thrown at a time 002. Use try
-catch statement for
processing The statement is processed 0002. Use throws to throw to the caller, let the caller use the try-catch statement to handle the exception ---------------------------- -------------------------------------------------- -------- Open source logging log4j tool





1. Use log4j to record the running process of the program and store it for later viewing

2.
Logs
and
classification .log classification
SQL log, exception log, business log 03.log4j is a
very good open source logging tool Log output format 3. Use log4j to record logs Step 01. Add log4j JAR file to the project 02. Create log4j.properties file 03. Configure log information 04. Use log4j to record log information 4. Detailed steps 01. Add JAR package 001 .Prepare the JAR package 002 in advance . Select the corresponding project in the tool (right click) → Properties (select) → Java Build Path (select) → Libraries (select) → Add External JARs.... (select) → find the pre-preparation Good JAR package → Open → OK 02. Write code 001. Right-click to add the file file. *The file name suffix is ​​(properties) *The comment in log4j is (##) 03. Use the log file
















Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326107189&siteId=291194637