当前位置: 首页 > news >正文

俄罗斯网站开发百度企业网盘

俄罗斯网站开发,百度企业网盘,帮人做违法网站,怎么选择赣州网站建设1.异常的概念与体系结构 1.1在Java中,将程序执行过程中发生的不正常行为称为异常.比如 1.算数异常 public class Main1 {public static void main(String[] args){System.out.println(10/0);} } //异常信息为:Exception in thread "main" java.lang.ArithmeticExc…

1.异常的概念与体系结构

1.1在Java中,将程序执行过程中发生的不正常行为称为异常.比如

1.算数异常

public class Main1 {public static void main(String[] args){System.out.println(10/0);}
}
//异常信息为:Exception in thread "main" java.lang.ArithmeticException: / by zero
//	            at Object1.work.error.Main1.main(Main1.java:5)

 2.数组越界异常

public class Main1 {public static void main(String[] args){int[] auto = new int[10];System.out.println(auto[100]);}
}
//异常信息为:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100
//	            at Object1.work.error.Main1.main(Main1.java:6)

3.空指针异常 

public class Main1 {public static void main(String[] args){int[] arr = null;System.out.println(arr.length);}
}
//异常信息为:Exception in thread "main" java.lang.NullPointerException
//	          at Object1.work.error.Main1.main(Main1.java:6)

上述过程中我们可以看见Java中不同类型的异常都有,不同的描述方式

1.2异常的体系结构

异常的种类繁多,为了对不同异常或者错误进行很好的分类管理,Java内部维护了一个异常的专门体系结构:

大致可以分为两个大方向

Throwable:异常的主类

Error:是指Java虚拟机无法解决的严重问题

Exception:异常产生后程序员可以通过代码进行处理,是程序继续执行. 

1.3异常的分类

异常可能在编译的时候发生,也可能在程序运行的时候发生,但根据时间的不同,异常可以被分类为:

1.编译时的异常(受检查异常)

2.运行时的异常(非受检查异常)

2.异常处理

2.1防御式编程

1.LBYL:Look Before You Leap.在操作前就做充分的检查

public class Main1 {public static void main(String[] args){boolean ret = false;if(!ret){//处理登录异常的相关代码return;}//登录游戏if(!ret){//处理游戏匹配异常的相关代码return;}//进入游戏}
}

像上述代码就是属于,防御型的异常处理,在我们为了防止异常的出现提前设置好相应异常的处理方式. 这种代码格式会使正常的代码和处理错误的代码混和在一起,显的代码比较混乱

2.EAFP:先操作,遇到问题在处理,即:事后认错型

这种操作的优势在于:正常的流程和错误的流程是分开操作的,我们更加关注正常流程

在java中,异常处理的主要的5个关键字分别是: throw,try,catch,final,throws.

2.2异常的抛出

在编写程序的时候,如果程序出现错误,我们需要把错误信息返回给此信息的调用者

在Java中可以通过throw关键字,抛出一个指定的异常对象,把错误信息告诉调用者

throw new xxxxException("异常产生的原因");

需求:实现一个获取数组中任意位置元素的方法

public class Main2 {public static int getElement(int[] array, int index){if(null == array){throw new NullPointerException("传递的数组为 null");}if(index < 0 || index >=array.length){throw new IndexOutOfBoundsException("数组索引越界");}return array[index];}public static void main(String[] args) {int[] array = {1,2,3,4,5};getElement(array,9);}
}

当array数组为"null"和数组索引越界时都会发生报错报错信息是根据各自抛出的throw中的内容.

注意事项:

1.throw必须写在方法体的内部

2.抛出的对象必须是Exception或者是Exception的子类对象

3.如果抛出的是RunTimeException或者是RunTimeException的子类,我们可以不用处理交给jvm处理就行

4.如果抛出的异常是编译的异常,必须要用户处理,否则无法通过编译

5.异常一旦抛出后面的代码是不会继续执行的

2.3异常的捕获

异常的捕获,也就是异常的具体的处理方式,通常有两种,throws以及try-catch捕获处理

2.31 异常声明throws

当方法中抛出编译异常的时候,用户不想处理该异常,此时可以借助throws将异常抛给方法的调用者来处理,就是说:当前方法不处理异常,提醒方法的调用者来处理该异常.

单独使用throw出现异常会让程序终止

public class Main3 {private static double divide(double a,double b){if(b == 0){throw new ArithmeticException("不能以0为除数");}return a/b;}public static void main(String[] args) {System.out.println(divide(10,0));}
}
// 输出结果:
// Exception in thread "main" java.lang.ArithmeticException: 不能以0为除数
// 	at Object1.work.error.Main3.divide(Main3.java:3)
// 	at Object1.work.error.Main3.main(Main3.java:7)

使用try-catch进行结合处理 

public class Main3 {private static double divide(double a,double b){if(b == 0){throw new ArithmeticException("不能以0为除数");}return a/b;}public static void main(String[] args) {System.out.println("抛出异常");try{//把也能抛出异常的代码放进try块中System.out.println(divide(10,0));}catch(ArithmeticException e){//捕获异常并打印异常信息System.out.println("捕获到的异常信息为:"+e.getMessage());}System.out.println("异常抛出后");}
}
// 输出结果:
// 抛出异常
// 捕获到的异常信息为:不能以0为除数
// 异常抛出后

可以看出程序是正常进行的并没有影响的程序的正常进行,可以理解为,自己抛出的异常被自己给处理掉了.

public class Main3 {private static double divide(double a,double b){if(b == 0){throw new ArithmeticException("不能以0为除数");}return a/b;}private static void func1(){divide(10,0);}private static void func2(){//func2()调用了func1(),如果func1()抛出了异常,func2()也会抛出异常func1();}public static void main(String[] args) {System.out.println("抛出异常");try{//把也能抛出异常的代码放进try块中func2();}catch(ArithmeticException e){//捕获异常并打印异常信息System.out.println("捕获到的异常信息为:"+e.getMessage());}System.out.println("异常抛出后");}
}

也可以通过方法之间进行调用先调用func2()通过func2()在去调用func1() 

 

public class Main4 {public static void main(String[] args) {try{int a = 10/0;}catch(ArithmeticException e){System.out.println("发生算数异常");}catch(NullPointerException e){System.out.println("发生空指针异常");}catch(ArrayIndexOutOfBoundsException e){System.out.println("发生数组越界异常");}}
}

产生的异常如果try下面出现多个catch它会依次进行匹配匹配到相应的错误信息后会打印出相关的错误内容,如果catch中没有相对应的异常时,就会交给jvm处理程序终止.

public class Main5 {public static void main(String[] args) {int[] arr = new int[5];try{System.out.println(arr[6]);}catch(ArithmeticException e){System.out.println("发生算数异常");} catch(NullPointerException e){System.out.println("发生空指针异常");}catch(ArrayIndexOutOfBoundsException e){System.out.println("发生数组越界异常");}catch(Exception e){System.out.println("发生其他异常");}}
}
//输出结果
//发生数组越界异常
public class Main5 {public static void main(String[] args) {int[] arr = new int[5];try{System.out.println(arr[6]);}catch(ArithmeticException e){System.out.println("发生算数异常");} catch(NullPointerException e){System.out.println("发生空指针异常");}catch(Exception e){System.out.println("发生其他异常");}}
}
//输出:
//发生其他异常
public class Main5 {public static void main(String[] args) {int[] arr = new int[5];try{System.out.println(arr[6]);}catch(Exception e){System.out.println("发生其他异常");}catch(ArithmeticException e){System.out.println("发生算数异常");} catch(NullPointerException e){System.out.println("发生空指针异常");}}
}
//发生报错无法正常进行

 

 通过上述代码进行比较:我们可以看出我们可以使用Exception来进行处理抛出的异常,但是用Exception是无法准确表达异常信息的一般我们是不建议使用的,还有就是Exception是上面三个异常的父类,而在catch时 Exception是是不能放在它子类前面的否则会发生报错

 

http://www.ds6.com.cn/news/44176.html

相关文章:

  • 如何申请免费域名做网站清远头条新闻
  • 南阳关键词优化抖音关键词排名优化
  • 商场网站建设模板推广普通话图片
  • 建站系统模板百度下载免费安装最新版
  • 惠州企业网站设计推广链接让别人点击
  • 网站建设课程教学改革福州短视频seo网红
  • 网站建设开发有限公司关键词如何优化排名
  • wordpress获取当前文章id宣城网站seo
  • 自己做的网站如何兼容免费建站的网站哪个好
  • 给客户做网站建设方案汕头百度关键词推广
  • 郑州知名网站建设公司云计算培训费用多少钱
  • 如何在网站上做网上亮照百度搜索关键词排名优化推广
  • 网络直播运营需要学什么一键优化表格
  • 广州网站制作公司联系方式网站营销软文
  • 城建设委官方网站郑州seo优化外包
  • dw制作wap网站怎么做百度客服人工
  • 做购物车网站多少钱站长推荐
  • 自贡网络推广hyein seo
  • 网站设计制作报价图片欣赏廊坊网站建设公司
  • 重庆 网站定制营销型企业网站案例
  • 张家港哪家做企业网站北京网站
  • 衡阳网站备案成都优化官网公司
  • 广东汕头最新消息汤阴县seo快速排名有哪家好
  • 权威的大连网站建设网建公司
  • wordpress不能换行seo每日工作
  • 购物建设网站费用百度站长收录入口
  • 海淀公安网站备案办理在线网页制作工具
  • 电子商务网站建设方式山东公司网站推广优化
  • 石家庄做网站排名公司哪家好app拉新平台
  • 网站建设可以在家做吗株洲seo优化报价