找回密码
 立即注册
printlnSystemDemoout | 企业管理 2022-09-23 116 0star收藏 版权: . 保留作者信息 . 禁止商业使用 . 禁止修改作品
版权声明:自创著作,谢绝转发!否则将追查法律责任。
异常的概念
指程序中止履行的一种命令流
publicclassDemo01
{
publicstaticvoidmain(Stringargs)
{
inti=0;
intj=10;
System.out.println(程序代码履行之前~~~);
System.out.println(i/j);
System.out.println(程序代码履行以后~~~);
}
}
如今我们反过来哈,j/i
publicclassDemo01
{
publicstaticvoidmain(Stringargs)
{
inti=0;
intj=10;
System.out.println(程序代码履行之前~~~);
System.out.println(j/i);
System.out.println(程序代码履行以后~~~);
}
}
异常一旦发作,则在异常发作处以后的一切代码都不再被履行了。
异常的分类
只需一发作了异常,则肯定会抛出一个异常类的实例化目标。
面试问题:RuntimeException与Exception的区别:
·RuntimeException表明异常可以由JVM进行管理
·Exception:表明用户可以自行管理
·实际上两个异常并没有太多的区别,用户都是可以管理的。
·Error:不能由用户自行管理的异常。
捕获异常(1)
publicclassDemo02
{
publicstaticvoidmain(Stringargs)
{
inti=0;
intj=10;
System.out.println(程序代码履行之前~~~);
try
{
//下面跟上可能发作错误的代码段
System.out.println(j/i);
System.out.println(-------------------);
}
catch(ArithmeticExceptionae)
{
//进行异常管理
System.out.println(运算发作了异常~~~);
}
System.out.println(程序代码履行以后~~~);
}
}
参加异常管理以后,程序可以正常的履行完,也可以正常的捕获错误了,但是在try句子中,在发作异常的当地就进行跳转了,所以异常发作句子以后的代码不会被履行。
假如程序中有多个异常要管理呢?
注意:一个新的操作:将字符串的内容变为数字。
publicclassDemo03
{
publicstaticvoidmain(Stringargs)
{
Stringstr=123;
//将字符串变为数字
inti=Integer.parseInt(str);
System.out.println(i
2);
}
}
在此代码当中,请求字符串的内容有必要是由数字构成的。
假如不全是数字,我们来看下作用
publicclassDemo03
{
publicstaticvoidmain(Stringargs)
{
Stringstr=123a;
//将字符串变为数字
inti=Integer.parseInt(str);
System.out.println(i
2);
}
}
请求:
请求可以在JAVA运行程序的后边参加运行时参数,参数为两个数相除的内容。
publicclassDemo04
{
publicstaticvoidmain(Stringargs)
{
//从运行时处接收参数
inti=Integer.parseInt(args);
intj=Integer.parseInt(args);
System.out.println(程序代码履行之前~~~);
try
{
//下面跟上可能发作错误的代码段
System.out.println(i/j);
System.out.println(-------------------);
}
catch(ArithmeticExceptionae)
{
//进行异常管理
System.out.println(运算发作了异常~~~);
}
System.out.println(程序代码履行以后~~~);
}
}
在以上的程序当中可能存在那些问题呢?
·假如没有输入参数的时候,则会有错误:ArrayIndexOutOfBoundsException(数组下标越界)
·假如输入的参数的内容不是数字,则会有错误:NumberFormatException(数字格式化异常)
·假如输入的被除数为零,则会有错误:ArithmeticException(算术异常)
则此程序代码只有一个catch肯定是不行的,需求管理多个异常。
publicclassDemo05
{
publicstaticvoidmain(Stringargs)
{
//从运行时处接收参数
inti=0;
intj=0;
System.out.println(程序代码履行之前~~~);
try
{
i=Integer.parseInt(args);
j=Integer.parseInt(args);
//下面跟上可能发作错误的代码段
System.out.println(i/j);
System.out.println(-------------------);
}
catch(ArithmeticExceptionae)
{
//进行异常管理
System.out.println(ae);
System.out.println(1.运算发作了异常~~~);
}
catch(NumberFormatExceptionne)
{
System.out.println(ne);
System.out.println(2.输入的内容不是数字~~~);
}
catch(ArrayIndexOutOfBoundsExceptionae)
{
System.out.println(ae);
System.out.println(3.输入的参数个数不正确~~~);
}
System.out.println(程序代码履行以后~~~);
}
}
以上的程序只需是有异常发作了,则会自动找到对应的catch句子,分别进行管理,但是一个程序可能会存在各种问题,那么有可能全面的catch都写出来吗?
ArithmeticException是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
.....

(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
.....)

[下载]10400905315.rar




上一篇:[零基础学JAVA]Java SE面向对象部分-19.面向对象高级(07)
下一篇:[零基础学JAVA]Java SE面向对象部分-21.包及访问权限