摘要:韦东山android视频学习笔记 

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;
}
}

编译运行:

相关代码存放在github,可以下载https://github.com/zzb2760715357/100ask

第2课第5节_Java面向对象编程_异常_P【学习笔记】的更多相关文章

  1. 第2课第3节_Java面向对象编程_继承性_P【学习笔记】

    摘要:韦东山android视频学习笔记  面向对象程序的三大特性之继承性:继承性的主要作用就是复用代码.继承性也有一定的限制,如图一 图一 1.我们在第2课第2节_Java面向对象编程_封装性_P 中 ...

  2. 第2课第4节_Java面向对象编程_多态性_P【学习笔记】

    摘要:韦东山android视频学习笔记  面向对象程序的三大特性之继承性: 1.向上转换:只能定义被子类覆写的方法,不能调用在子类中定义的方法. class Father { private int ...

  3. 第2课第7节_Java面向对象编程_内部类_P【学习笔记】

    摘要:韦东山android视频学习笔记  1.什么是内部类:在类的内部定义一个类,内部类可以访问类的私有属性 class Outer{ ; class Inner{ public void print ...

  4. 第2课第2节_Java面向对象编程_封装性_P【学习笔记】

    摘要:韦东山android视频学习笔记  面向对象程序的三大特性之封装性:把属性和方法封装在一个整体,同时添加权限访问. 1.封装性的简单程序如下,看一下第19行,如果我们不对age变量进行权限的管控 ...

  5. 第2课第1节_Java面向对象编程_类的引入_P【学习笔记】

    摘要:韦东山android视频学习笔记 1. 面向对象编程的引入,我们先写一个简单的程序输出张三,李四的名字.代码如下,假如,现在我们要在名字前面添加籍贯广东,那样岂不是每个printf语句都得修改添 ...

  6. 第2课第6节_Java面向对象编程_包和权限_P【学习笔记】

    摘要:韦东山android视频学习笔记  1.使用package定义编译的时候存放的位置 package a.b.c.d; public class Package { public static v ...

  7. 类和对象:面向对象编程 - 零基础入门学习Python037

    类和对象:面向对象编程 让编程改变世界 Change the world by program 经过上节课的热身,相信大家对类和对象已经有了初步的认识,但似乎还是懵懵懂懂:好像面向对象编程很厉害,但不 ...

  8. .net 4.0 面向对象编程漫谈基础篇读书笔记

    话说笔者接触.net 已有些年头,做过的项目也有不少,有几百万的,也有几十万的,有C/S的,也有B/S的.感觉几年下来,用过的框架不少,但是.net的精髓一直没有掌握.就像学武之人懂得各种招式,但内功 ...

  9. Python 进阶_OOP 面向对象编程_组合与继承

    #目录 前言 组合 派生 通过继承来覆盖重载方法 最常用的重载场景实例方法的重载 从标准类中派生类方法的重载 前言 我们定义一个类是希望能够把类当成模块来使用,并把类嵌入到我们的应用代码中,与其他的数 ...

随机推荐

  1. Mysql慢查询日志以及优化

    慢查询日志设置 当语句执行时间较长时,通过日志的方式进行记录,这种方式就是慢查询的日志. 1.临时开启慢查询日志(如果需要长时间开启,则需要更改mysql配置文件) set global slow_q ...

  2. VSCode 控制台面板输出乱码 字符编码问题 PHP --已解决

    首先上一张效果图,看看是不是你想要的效果. 第一步: 按F1,输入settings.json,添加 "code-runner.runInTerminal": true, 第二步:将 ...

  3. CentOS8-在hyper-V安装选项

    安装选项select Server with a GUI.后重启卡在黑屏无法启动. 后改选: Select software to be installed.  Choose the Workstat ...

  4. JMeter性能测试,入门

    原文转自:https://blog.csdn.net/lovesoo/article/details/78579547 Apache JMeter是一款纯java编写负载功能测试和性能测试开源工具软件 ...

  5. 爬虫之scrapy框架的crawlspider

    一,介绍 CrawlSpider其实是Spider的一个子类,除了继承到Spider的特性和功能外,还派生除了其自己独有的更加强大的特性和功能.其中最显著的功能就是”LinkExtractors链接提 ...

  6. Linux_安装maven

    安装maven 1.首先要已经安装JDK 2.下载安装包,可以安装包下: 下载地址:https://mirrors.cnnic.cn/apache/maven/ wget https://mirror ...

  7. python 列表的属性和方法整理

    list属性和方法 补一个超实用的函数 envmerate(列表名, [start=0])  作用:  将一个可遍历的数据对象(如列表.元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用 ...

  8. 摘:J2EE开发环境搭建(1)——安装JDK、Tomcat、Eclipse

    J2EE开发环境搭建(1)——安装JDK.Tomcat.Eclipse 1:背景 进公司用SSH(Struts,spring和hibernate)开发已经有两个月了,但由于一 直要么只负责表示层的开发 ...

  9. 使用js对社会信用代码进行正则验证

    注:参考了该博客(https://blog.csdn.net/qq_37142340/article/details/80695187)进行了一些修改,本文验证使用在微信小程序上. 直接贴代码: va ...

  10. 深度学习Keras框架笔记之核心层基类

    Keras的Layers,就是构成网络的每一层.Keras实现了很多层,包括核心层.卷基层.RNN网络层等诸多常用的网络结构.下面开介绍核心层中包含了哪些内容.因为这个核心层我现在还没有全部用到,所以 ...