}finally{
释放资源的代码
不管是否发生异常,都会执行finally中的语句
}
package review.ExceptionDemo;
public class demo6 {
public static void main(String[] args) {
System.out.println(“before”);
int a = 5;
int b = 0;
//try…catch处理一些可能会出现问题的代码
try {
System.out.println(a/b);
}catch (ArithmeticException a1){
System.out.println(“除数不能为0”);
}
System.out.println(“after”);
}
}
当然,一段代码中也可能有多个异常,有几个异常就需要写几个catch语句
package review.ExceptionDemo;
public class demo6 {
public static void main(String[] args) {
System.out.println(“before”);
int a = 5;
int b = 0;
try{
System.out.println(a / b);
int[] arr = {1, 2, 3, 4, 5};
System.out.println(arr[5]);
}catch (ArithmeticException a1){
System.out.println("除数不能为0 ");
}catch (ArrayIndexOutOfBoundsException a2){
System.out.println(“数组下标越界”);
}
}
}
注意:
- 异常的类型尽可能的准确,如果能明确异常的种类时,就不要用该异常的父类
比如IOException可以处理的异常,不要用Exception来概括
-
当多个异常之间是平级的关系时,多个catch语句没有前后顺序关系,谁在前谁在后都行。但是一旦出现了父类继承的关系,父类异常必须在后。
-
一旦try里面的代码出现了问题,就会去匹配catch里面的异常,继续执行catch中的语句。try里面的代码就停在了出现问题的那一步。但是try-catch语句后面的代码可以正常执行。
package review.ExceptionDemo;
public class demo6 {
public static void main(String[] args) {
try{
System.out.println(“before”);
int[] arr = {1, 2, 3, 4, 5};
System.out.println(arr[5]);
System.out.println(“after”);
} catch (ArrayIndexOutOfBoundsException a2){
System.out.println(“数组下标越界”);
System.out.println(“继续执行”);
}finally{
System.out.println(“不管是否发生异常,都会执行”);
}
System.out.println(“之后的程序代码正常执行”);
}
}
package review.ExceptionDemo;
public class demo6 {
public static void main(String[] args) {
try{
System.out.println(“before”);
int[] arr = {1, 2, 3, 4, 5};
System.out.println(arr[3]);
System.out.println(“after”);
} catch (ArrayIndexOutOfBoundsException a2){
System.out.println(“数组下标越界”);
System.out.println(“继续执行”);
}finally{
System.out.println(“不管是否发生异常,都会执行”);
}
}
}
JDK7的新特性,可以一次性处理多个异常
try{
可能会出现问题的代码
}catch(异常类名1|异常类名2|… 变量名){
处理异常的语句
}
public class demo7 {
public static void main(String[] args) {
int a = 3;
int b = 0;
try{
System.out.println(a / b);
int[] arr = {1, 2, 3, 4, 5};
System.out.println(arr[5]);
}catch (ArithmeticException|ArrayIndexOutOfBoundsException a1){
System.out.println(“发生异常”);
}
}
}
注意:
-
不同的异常处理的方式是一样的,虽然简洁但是不能针对性的进行处理
-
多个异常的类型之间必须是平级的关系
catch语句块中的方法
package review.ExceptionDemo;
public class demo8 {
public static void main(String[] args) {
int a = 10;
int b = 0;
try {
System.out.println(a/b);
}catch (ArithmeticException a1){
//方法一
String message = a1.getMessage();
//打印出现问题的原因
System.out.println(message);
//方法二
String s = a1.toString();
System.out.println(s);
//方法三
a1.printStackTrace();
}
System.out.println(“之后程序的代码正常执行”);
}
}
这里打印的异常信息,与没有做异常处理时,JVM自动在控制台给出的异常信息类似。但是做了异常处理后,可以让程序后面的代码正常的执行,不会因为这个异常而导致整个程序的终止。
printStackTrace()
这个方法是从java.lang.Throwable类继承过来的,会将具体的错误发生的栈轨迹信息打印在控制台上
package test.ExceptionDemo;
public class ExceptionDemo3 {
public static void test1() throws Exception{
throw new Exception(“Exception”);
}
public static void test2() throws Exception{
test1();
}
public static void main(String[] args) {
try{
test2();
} catch(Exception e) {
e.printStackTrace();
}
}
}
在方法test1()中抛出异常,方法test2()中调用方法test1(),在main方法中捕获异常,并且打印栈轨迹信息。因此,输出依次展示了test1() —> test2() —> main的过程。
throws
有时候我们可以针对一些异常进行处理,但是也有的时候,我们无法处理这个异常
对于这种情况,为了保证程序的正常执行,可以使用try-catch-finally之外的第二个处理方法
throws抛出异常,用在方法的声明上,表明当前方法不处理异常,等实际调用的时候,由该方法的调用者来处理异常
格式:
throws 异常类名
修饰符 返回值类型 方法名(参数) throws 异常类名1,异常类名2 … { }
注意:
-
抛出编译时异常,抛出异常的方法内可以不做处理,但是调用该方法的时候必须做处理,否则编译时就会报错
-
抛出运行时异常,调用时可以不做任何处理。编译时不会报错,但是程序遇到错误会终止
-
最好抛出详细的异常类型,也可以抛出更大的异常类型,但是处理的时候要对应上。比如throws Exception,则catch语句中是 Exception e
-
尽量不要在main方法上抛出,如果在main方法上抛出,就会交给JVM处理,遇到异常时程序会直接停止
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class demo9 {
public static void main(String[] args) {
System.out.println(“程序开始”);
try {
fun1();
}catch (Exception pe){
pe.printStackTrace();
}
try {
fun2();
}catch (ArithmeticException ae){
ae.printStackTrace();
}
System.out.println(“程序结束”);
}
public static void fun2() throws ArithmeticException{
int a = 10;
int b =0;
System.out.println(a/b);
}
public static void fun1() throws ParseException {
String s = “2021-10-18”;
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
Date date = sdf.parse(s);
System.out.println(date);
}
}
发生异常之外的代码都正常的执行了
throw
throws和throw的区别:
-
throws用来声明一个方法可能产生的所有异常,throw用来抛出一个具体的异常类型。
-
throw语句用在方法体内,表示抛出异常,由方法体内的语句处理。
throws语句用在方法声明后面,表示抛出异常,由该方法的调用者来处理
- throws只是表明一种可能性,不一定会抛出异常
throw则是一定会抛出一个具体的异常
throw举例
package review.ExceptionDemo;
public class demo10 {
public static void main(String[] args) {
fun();
}
public static void fun(){
int a = 10;
int b = 0;
if(b == 0){
System.out.println(“报错,除数不能为0”);
throw new ArithmeticException();
}else {
System.out.println(a/b);
}
}
}
throws举例
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)
Spring全套教学资料
Spring是Java程序员的《葵花宝典》,其中提供的各种大招,能简化我们的开发,大大提升开发效率!目前99%的公司使用了Spring,大家可以去各大招聘网站看一下,Spring算是必备技能,所以一定要掌握。
目录:
部分内容:
Spring源码
- 第一部分 Spring 概述
- 第二部分 核心思想
- 第三部分 手写实现 IoC 和 AOP(自定义Spring框架)
- 第四部分 Spring IOC 高级应用
基础特性
高级特性 - 第五部分 Spring IOC源码深度剖析
设计优雅
设计模式
注意:原则、方法和技巧 - 第六部分 Spring AOP 应用
声明事务控制 - 第七部分 Spring AOP源码深度剖析
必要的笔记、必要的图、通俗易懂的语言化解知识难点
脚手框架:SpringBoot技术
它的目标是简化Spring应用和服务的创建、开发与部署,简化了配置文件,使用嵌入式web服务器,含有诸多开箱即用的微服务功能,可以和spring cloud联合部署。
Spring Boot的核心思想是约定大于配置,应用只需要很少的配置即可,简化了应用开发模式。
- SpringBoot入门
- 配置文件
- 日志
- Web开发
- Docker
- SpringBoot与数据访问
- 启动配置原理
- 自定义starter
微服务架构:Spring Cloud Alibaba
同 Spring Cloud 一样,Spring Cloud Alibaba 也是一套微服务解决方案,包含开发分布式应用微服务的必需组件,方便开发者通过 Spring Cloud 编程模型轻松使用这些组件来开发分布式应用服务。
- 微服务架构介绍
- Spring Cloud Alibaba介绍
- 微服务环境搭建
- 服务治理
- 服务容错
- 服务网关
- 链路追踪
- ZipKin集成及数据持久化
- 消息驱动
- 短信服务
- Nacos Confifig—服务配置
- Seata—分布式事务
- Dubbo—rpc通信
Spring MVC
目录:
部分内容:
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
ba 也是一套微服务解决方案,包含开发分布式应用微服务的必需组件,方便开发者通过 Spring Cloud 编程模型轻松使用这些组件来开发分布式应用服务。
- 微服务架构介绍
- Spring Cloud Alibaba介绍
- 微服务环境搭建
- 服务治理
- 服务容错
- 服务网关
- 链路追踪
- ZipKin集成及数据持久化
- 消息驱动
- 短信服务
- Nacos Confifig—服务配置
- Seata—分布式事务
- Dubbo—rpc通信
[外链图片转存中…(img-DXv4Lz8x-1713497587935)]
[外链图片转存中…(img-aj8g7Ems-1713497587936)]
Spring MVC
目录:
[外链图片转存中…(img-LbTbgSl4-1713497587937)]
[外链图片转存中…(img-vsGf9sBC-1713497587937)]
[外链图片转存中…(img-md0OJhtt-1713497587938)]
部分内容:
[外链图片转存中…(img-ukC8mQjQ-1713497587939)]
[外链图片转存中…(img-2426uB7i-1713497587940)]
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!