(Copyright statement: self-created works, please do not forward! Otherwise, legal responsibility will be pursued.
exception concept
Refers to a command flow in which the program suspends execution
publicclassDemo01
{
publicstaticvoidmain(Stringargs)
{
inti=0;
intj=10;
System.out.println(before the program code is executed~~~);
System.out.println(i/j);
System.out.println(after the program code is executed~~~);
}
}
Now let's turn around, j/i
publicclassDemo01
{
publicstaticvoidmain(Stringargs)
{
inti=0;
intj=10;
System.out.println(before the program code is executed~~~);
System.out.println(j/i);
System.out.println(after the program code is executed~~~);
}
}
Once the abnormality occurs, all codes after the abnormality occurrence are no longer executed.
abnormal classification
As long as an exception occurs, an instantiation target of the exception class will definitely be thrown.
Interview question: Difference between RuntimeException and Exception:
RuntimeException indicates that the exception can be managed by the JVM
Exception: Indicates that the user can self-manage
·In fact, there is not much difference between the two exceptions, and users can manage them both.
Error: An exception that cannot be managed by the user.
catch exception(1)
publicclassDemo02
{
publicstaticvoidmain(Stringargs)
{
inti=0;
intj=10;
System.out.println(before the program code is executed~~~);
try
{
//Follow up with the code snippet where the error may occur
System.out.println(j/i);
System.out.println(-------------------);
}
catch(ArithmeticExceptionae)
{
//do exception management
System.out.println(The operation has an exception~~~);
}
System.out.println(after the program code is executed~~~);
}
}
After participating in the exception management, the program can be executed normally and the error can be caught normally, but in the try sentence, the jump is made at the place where the exception occurs, so the code after the exception occurs sentence will not be executed.
What if the program has multiple exceptions to manage?
Note: A new action: turns the content of a string into a number.
publicclassDemo03
{
publicstaticvoidmain(Stringargs)
{
Stringstr=123;
// convert string to number
inti=Integer.parseInt(str);
System.out.println(i
2);
}
}
In this code, the content of the request string must be composed of numbers.
If it's not all numbers, let's look at the effect
publicclassDemo03
{
publicstaticvoidmain(Stringargs)
{
Stringstr=123a;
// convert string to number
inti=Integer.parseInt(str);
System.out.println(i
2);
}
}
ask:
The request can add runtime parameters after the JAVA running program, and the parameter is the content of the division of two numbers.
publicclassDemo04
{
publicstaticvoidmain(Stringargs)
{
//Receive parameters from runtime
inti=Integer.parseInt(args);
intj=Integer.parseInt(args);
System.out.println(before the program code is executed~~~);
try
{
//Follow up with the code snippet where the error may occur
System.out.println(i/j);
System.out.println(-------------------);
}
catch(ArithmeticExceptionae)
{
//do exception management
System.out.println(The operation has an exception~~~);
}
System.out.println(after the program code is executed~~~);
}
}
What problems may exist in the above procedures?
If there is no input parameter, there will be an error: ArrayIndexOutOfBoundsException (array index out of bounds)
·If the content of the input parameter is not a number, there will be an error: NumberFormatException (number format exception)
·If the input dividend is zero, there will be an error: ArithmeticException (arithmetic exception)
Then this program code has only one catch is definitely not enough, it needs to manage multiple exceptions.
publicclassDemo05
{
publicstaticvoidmain(Stringargs)
{
//Receive parameters from runtime
inti=0;
intj=0;
System.out.println(before the program code is executed~~~);
try
{
i=Integer.parseInt(args);
j=Integer.parseInt(args);
//Follow up with the code snippet where the error may occur
System.out.println(i/j);
System.out.println(-------------------);
}
catch(ArithmeticExceptionae)
{
//do exception management
System.out.println(ae);
System.out.println(1. An exception occurred in the operation~~~);
}
catch(NumberFormatExceptionne)
{
System.out.println(ne);
System.out.println(2. The input is not a number~~~);
}
catch(ArrayIndexOutOfBoundsExceptionae)
{
System.out.println(ae);
System.out.println(3. The number of input parameters is incorrect~~~);
}
System.out.println(after the program code is executed~~~);
}
}
As long as the above program has an abnormal attack, it will automatically find the corresponding catch sentence and manage it separately, but there may be various problems in a program, so is it possible to write a comprehensive catch?
ArithmeticException is a subclass of Exception~
publicclassDemo06
{
publicstaticvoidmain(Stringargs)
{
oo8\Demo01.class
oo8\Demo01.java
oo8\Demo02.class
oo8\Demo02.java
oo8\Demo03.class
oo8\Demo03.java
oo8\Demo04.class
oo8\Demo04.java
oo8\Demo05.class
oo8\Demo05.java
oo8\Demo06.class
oo8\Demo06.java
oo8\Demo07.java
oo8\Demo08.class
oo8\Demo08.java
oo8\Demo09.class
oo8\Demo09.java
oo8\Demo10.class
oo8\Demo10.java
oo8\Demo11.class
oo8\Demo11.java
oo8\Demo12.class
oo8\Demo12.java
oo8\Demo13.class
oo8\Demo13.java
oo8\Demo14.class
oo8\Demo14.java
oo8\Math.class
oo8\MyException.class
.....)