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

收藏网站怎么做凡科网微信小程序

收藏网站怎么做,凡科网微信小程序,登录我的企业邮箱,网站如何做excel预览1.概念 在Java中,异常(Exception)是指程序在运行过程中发生的不正常情况 例如: 算数异常(ArithmeticException) 空指针异常(NullPointerException) 数组越界异常(ArrayIndexOutOfBoundsException) 根据上述的异常信息可以看出:每个具体的异…

1.概念

在Java中,异常(Exception)是指程序在运行过程中发生的不正常情况
例如:

  • 算数异常(ArithmeticException) 在这里插入图片描述
  • 空指针异常(NullPointerException) 在这里插入图片描述
  • 数组越界异常(ArrayIndexOutOfBoundsException) 在这里插入图片描述

根据上述的异常信息可以看出:每个具体的异常都有一个类来进行描述

2.异常的体系

在这里插入图片描述

Throwable:是所有错误(Error)和异常(Exception)的父类。它有两个主要的子类:ErrorException

Error:Error及其子类表示Java虚拟机(JVM)无法处理的严重问题。这些错误通常是不可恢复的,应用程序不应该尝试去捕捉这些错误

Exception:Exception及其子类表示应用程序可以捕获和处理的问题。Exception又可以分为两大类:

  • 1.检查型异常(Checked Exception)
  • 2.非检查型异常(Unchecked Exception)

3.错误(Error)

下面是栈溢出错误代码示例:

public class Demo {public void demo() {demo();}public static void main(String[] args) {Demo demo = new Demo();demo.demo();}
}

运行结果: Exception in thread "main" java.lang.StackOverflowError

在Java中,每次调用方法都会在虚拟机栈上为该方法开辟一个方法栈帧。上述代码中无限递归调用demo()方法就会无限地开辟方法栈帧。因为栈空间是有限的,所以就会抛出StackOverflowError错误。像这种错误一旦发生整个应用程序就会立刻崩溃,唯一的解决办法就是从根源上修改代码

4.异常(Exception)

4.1 检查型异常(Checked Exception)

检查型异常在编译阶段就会被检查,编译器强制要求程序员处理这些异常,比如:CloneNotSupportedException,IOException等。如果程序中抛出了这些异常,必须使用throws进行声明或者使用try-catch进行处理在这里插入图片描述

4.2 非检查型异常(Unchecked Exception)

非检查型异常在编译阶段不会被检查,是在运行时可能发生的异常,比如:ArithmeticException,NullPointerException,ArrayIndexOutOfBoundsException等。这些异常如果程序员不做处理,就会交给JVM进行处理在这里插入图片描述

5.异常的处理

5.1 throw

throw关键字用来显式抛出一个异常

public class Demo {public static int function(int num){if(num == 0){throw new ArithmeticException();}return 10 / num;}public static void main(String[] args){System.out.println(function(0));}
}

5.2 throws

throws关键字用于方法签名中,声明该方法可能抛出的异常。它主要用于处理检查型异常(Checked
Exceptions),即那些在编译时需要被处理的异常。通过使用throws,方法可以声明它不处理某些异常,而是将异常的处理责任传递给调用该方法的代码

在这里插入图片描述

5.3 try-catch

try-catch允许程序员捕捉并处理应用程序允许过程中可能出现的异常,从而防止程序因未处理的异常而中断

当程序中抛出非检查型异常时,如果程序员不介入解决,就会交由JVM来处理。当JVM处理异常时会中断当前应用程序

public class Demo {public static void function(int num){if(num == 0){throw new ArithmeticException();}System.out.println("程序继续执行");}public static void main(String[] args){function(0);}
}

运行结果:
在这里插入图片描述
使用try-catch处理该异常:

public class Demo {public static void function(int num){if(num == 0){throw new ArithmeticException();}}public static void main(String[] args){try {function(0);}catch (ArithmeticException e){System.out.println("成功捕捉到该异常并处理");}System.out.println("程序继续执行");}
}

运行结果:
在这里插入图片描述

注意:

  • 1.try块内抛出异常后的代码将不会被执行 在这里插入图片描述
  • 2.如果抛出异常类型与catch时异常类型不匹配,即异常不会被成功捕获,也就不会被处理,继续往外抛,直到JVM收到后中断程序——异常是按照类型来捕获在这里插入图片描述
  • 3 try中可能会抛出多个不同的异常对象,则必须用多个catch来捕获——即多种异常,多次捕获在这里插入图片描述
  • 4.如果异常之间具有父子关系,一定是子类异常在前catch父类异常在后catch,否则语法错误 在这里插入图片描述
  • 5.可以使用Exception来一次性捕获所有异常,实现一劳永逸(一般不推荐,不利于排查问题)

5.4 finally

finally是Java中异常处理机制的一部分,通常与try-catch块一起使用。无论try块中的代码是否抛出异常,finally块中的代码都会执行。在finally块中主要执行清理操作,如释放资源、关闭文件或数据库连接等
在这里插入图片描述

6.自定义异常

虽然Java中已经内置了非常多的异常类,但不一定完全符合用户的需求,所以用户也可以自定义自己期望的异常

6.1 定义方式

如果希望自定义非检查型异常,可以继承自RuntimeException及其子类

public class PasswordErrorException extends RuntimeException {public PasswordErrorException() {super();}public PasswordErrorException(String message) {super(message);}
}

如果希望自定义检查型异常,可以继承自IOException等检查型异常

public class PasswordErrorException extends IOException {public PasswordErrorException() {super();}public PasswordErrorException(String message) {super(message);}
}

6.2 使用自定义异常来实现登录功能

public class PasswordErrorException extends RuntimeException {public PasswordErrorException() {super();}public PasswordErrorException(String message) {super(message);}
}
public class UsernameErrorException extends RuntimeException{public UsernameErrorException(String message){super(message);}public UsernameErrorException(){super();}
}
public class Login {public void login(String username, String password){if(!"admin".equals(username) ){throw new UsernameErrorException("用户名错误");}if(!"123456".equals(password)){throw new PasswordErrorException("密码错误");}System.out.println("登录成功");}public static void main(String[] args) {Login login = new Login();try {login.login("admin", "123456");}catch (UsernameErrorException | PasswordErrorException e){//printStackTrace()方法可以打印出抛异常的具体位置e.printStackTrace();}System.out.println("程序继续执行");}
}
http://www.dtcms.com/wzjs/241297.html

相关文章:

  • 如何做资金盘网站百度推广方案怎么写
  • 长春专业网站制作成都网络推广运营公司
  • 宁波优化推广找哪家临沂seo网站管理
  • 国内网站免费服务器新手怎么学电商运营
  • 注册域名后怎么做网站乔拓云智能建站平台
  • 番禺网站建设怎样当前疫情十大热点
  • b2b网站建设价格啥是网络推广
  • 广州网站建设出售常熟网络推广
  • 做网站 阿里云和百度云哪个好seo主要是指优化
  • 只做彩票网站犯法吗谷歌下载官方正版
  • 如何用dw做网站淘宝seo是指什么
  • 分销商城搭建关键词优化公司排行
  • 自己做公司的网站手机搭建网站
  • 客服外包平台有哪些上海优化seo排名
  • bootstrap网站开发教程帮别人发广告赚钱平台
  • dw做一个小网站教程哪些网站可以免费发广告
  • 电商网站建设哪家公司好关键词智能优化排名
  • 备案成功后多久可以打开网站江西seo推广软件
  • 吕子乔做网站一段台词站长工具查询网站信息
  • 山东省政府办公厅马希军廊坊优化外包
  • 做网站用别人图片文章会侵权吗四川旅游seo整站优化站优化
  • 学校网站建设目的及功能定位惠州seo网络推广
  • 深圳知名网站建设公司优化人员是什么意思
  • 珠海网站建设哪家权威外贸订单一般在哪个平台接
  • 有域名之后怎么做网站建站平台
  • 网站里的个人中心下拉列表怎么做常见的网站推广方式
  • 手机企业网站怎么做网络营销一般月薪多少
  • 做酒店网站所用到的算法网站怎么注册
  • 做擦边球网站会不会违法呢深圳宝安seo外包
  • 网站详情页怎么做的发布软文的平台有哪些