第2课第5节_Java面向对象编程_异常_P
程序员文章站
2022-06-16 17:25:37
...
java的异常处理的原则如下:
1、我们先写一个没有对异常处理的程序,在进行除法运算的时候,除数是非零的话,运行时没有问题的,但是除数为零的时候,运行就会有问题,程序也不能往下执行(只打印了Begin of div)
public class Div{
public static void main(String args[]){
int m = Integer.parseInt(args[0]);
int n = Integer.parseInt(args[1]);
System.out.println("Begin of div");
int r = div(m,n);
System.out.println("end of div");
System.out.println(m+"/"+n+"="+r);
}
public static int div(int m,int n){
int r = m / n;
return r;
}
}
2、我们先写一个有对异常进行处理程序(自己处理异常),根据下面的运行结果,程序可以捕获到异常并且可以正常的执行.
public class Div2{
public static void main(String args[]){
int m = Integer.parseInt(args[0]);
int n = Integer.parseInt(args[1]);
System.out.println("Begin of div");
int r = div(m,n);
System.out.println("end of div");
System.out.println(m+"/"+n+"="+r);
}
public static int div(int m,int n){
int r = 0;
try {
r = m / n ;
}catch (ArithmeticException e){
System.out.println(e);
}finally{
System.out.println("This is finally of div");
}
return r;
}
}
3、我们写一个程序将异常抛出的类,这个抛出的异常是由main进行处理.
public class Div4{
public static void main(String args[]){
int m = Integer.parseInt(args[0]);
int n = Integer.parseInt(args[1]);
int r = 0;
System.out.println("Begin of div");
try {
r = div(m,n);
}catch (ArithmeticException e){
System.out.println(e);
}
System.out.println("end of div");
System.out.println(m+"/"+n+"="+r);
}
public static int div(int m,int n) throws ArithmeticException{
int r = 0;
r = m / n ;
return r;
}
}
4、如果在类的方法中如果处理了异常,那样在main方法中就不会对异常进行处理.
public class Div5{
public static void main(String args[]){
int m = Integer.parseInt(args[0]);
int n = Integer.parseInt(args[1]);
int r = 0;
System.out.println("Begin of div");
try {
r = div(m,n);
}catch (ArithmeticException e){
System.out.println(e);
}
System.out.println("end of div");
System.out.println(m+"/"+n+"="+r);
}
public static int div(int m,int n) throws ArithmeticException{
int r = 0;
try{
r = m / n ;
}catch(ArithmeticException e){
System.out.println("div :"+e);
}
return r;
}
}
5、如果在类的方法中如果处理了异常,同时在类方法中把异常抛出,那样main方法也可以捕获到异常.
public class Div6{
public static void main(String args[]){
int m = Integer.parseInt(args[0]);
int n = Integer.parseInt(args[1]);
int r = 0;
System.out.println("Begin of div");
try {
r = div(m,n);
}catch (ArithmeticException e){
System.out.println(e);
}
System.out.println("end of div");
System.out.println(m+"/"+n+"="+r);
}
public static int div(int m,int n) throws ArithmeticException{
int r = 0;
try{
r = m / n ;
}catch(ArithmeticException e){
System.out.println("div :"+e);
throw e;
}
return r;
}
}
6、现在我们上面第5个例子的代码,只有对这种算术运行的异常进行处理,如果我传入的参数个数不对,还有参数的格式也不对,程序是处理不了的。
为了修复上述的问题,我们添加对传入参数格式不对,还有传入参数个数不对这两种异常的处理。
public class Div7{
public static void main(String args[]){
int m = 0;
int n = 0;
int r = 0;
System.out.println("Begin of div");
try {
m = Integer.parseInt(args[0]);
n = Integer.parseInt(args[1]);
r = div(m,n);
}catch (ArithmeticException e){
System.out.println("main :" + e);
}catch (NumberFormatException e){
System.out.println("main :" + e);
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("main :" + e);
}
System.out.println("end of div");
System.out.println(m+"/"+n+"="+r);
}
public static int div(int m,int n) throws ArithmeticException{
int r = 0;
try{
r = m / n ;
}catch(ArithmeticException e){
System.out.println("div :"+e);
throw e;
}
return r;
}
}
7、在第6个例子继续优化,上面的程序目前只能对算术运算、参数格式还有参数个数不对的异常进行处理,其他的情况是无法处理的到的,我们可以添加对这些异常的父类RuntimeException来捕获异常.
public class Div8{
public static void main(String args[]){
int m = 0;
int n = 0;
int r = 0;
System.out.println("Begin of div");
try {
m = Integer.parseInt(args[0]);
n = Integer.parseInt(args[1]);
r = div(m,n);
}catch (ArithmeticException e){
System.out.println("main :" + e);
}catch (NumberFormatException e){ //去掉了参数个数的异常,仍然可以捕获到
System.out.println("main :" + e);
}catch (RuntimeException e){
System.out.println("main :" + e);
}
System.out.println("end of div");
System.out.println(m+"/"+n+"="+r);
}
public static int div(int m,int n) throws ArithmeticException{
int r = 0;
try{
r = m / n ;
}catch(ArithmeticException e){
System.out.println("div :"+e);
throw e;
}
return r;
}
}
8、对于“不可查异常”, 系统也会抛出它,写不写throws效果一样
public class Div9{
public static void main(String args[]){
int m = 0;
int n = 0;
int r = 0;
System.out.println("Begin of div");
try {
m = Integer.parseInt(args[0]);
n = Integer.parseInt(args[1]);
r = div(m,n);
}catch (ArithmeticException e){
System.out.println("main :" + e);
}catch (NumberFormatException e){
System.out.println("main :" + e);
}catch (RuntimeException e){
System.out.println("main :" + e);
}
System.out.println("end of div");
System.out.println(m+"/"+n+"="+r);
}
//public static int div(int m,int n) throws ArithmeticException{
public static int div(int m,int n){
int r = 0;
try{
r = m / n ;
}catch(ArithmeticException e){
System.out.println("div :"+e);
throw e;
}finally{
System.out.println("finally of div");
}
return r;
}
}
下一篇: flink