java异常
浅聊java异常
Excepection
- 这说明try语句块发生异常之后,try语句块中的剩余内容就不会再被执行了。
- 无论是否抛出异常如何finally还是会执行
- e.printStackTrace();//输出异常堆栈信息,在命令行打印异常信息在程序中出错的位置及原因。
StackTrace是什么
package com.liang.text;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@SpringBootTest
class TextApplicationTests {
@Test
void contextLoads() {
}
public static void main(String[] args) throws FileNotFoundException {
Integer a = 10;
Integer b = null;
try {
int c = devsion(a, b);
System.out.println("没有报异常正常执行结果"+c);
}catch (NullPointerException e){//参数e只能在对应catch内使用
System.out.println("捕获异常进入catch:NullPointerException");
e.printStackTrace();//输出报错异常堆栈信息
System.out.println("-------------f分割线----------");
StackTraceElement[] stackTrace = e.getStackTrace();//alt+回车快速生成
for (StackTraceElement stackTraceElement : stackTrace) {
String className = stackTraceElement.getClassName();
System.out.println("className的类名"+className);
}
}catch (ArrayIndexOutOfBoundsException ae){
System.out.println("进入catch:ArrayIndexOutOfBoundsException");
ae.printStackTrace();
}
finally {
System.out.println("执行finally");
}
}
/**
* 除法
* @param a
* @param b
* @return
*/
public static Integer devsion(Integer a,Integer b){
return a/b;
}
}
捕获异常进入catch:NullPointerException
-------------f分割线----------
className的类名com.liang.text.TextApplicationTests
className的类名com.liang.text.TextApplicationTests
执行finally
java.lang.NullPointerException
at com.liang.text.TextApplicationTests.devsion(TextApplicationTests.java:50)
at com.liang.text.TextApplicationTests.main(TextApplicationTests.java:20)
Process finished with exit code 0
- throws是用于在方法声明抛出的异常,而throw是用于抛出异常。
Java异常处理流程
-
Java 的异常处理是通过 5 个关键词来实现的:try、catch、throw、throws 和 finally。
-
finally块一般是用来关闭(释放)物理资源(数据库连接,网络连接,磁盘文件等)。无论是否发生异常,资源都必须进行关闭。
-
异常的处理机制分为声明异常,抛出异常和捕获异常。
-
声明异常:捕获那些需要处理的异常,将不知道如何处理的异常传递下去。传递异常可以在方法声明使用throws抛出异常。
- 非检查异常(Error、RuntimeException 或它们的子类)不可使用 throws 关键字来声明要抛出的异常。
- 一个方法出现编译时异常,就需要 try-catch/ throws 处理,否则会导致编译错误。
-
捕获异常: 程序通常在运行之前不报错,但是运行后可能会出现某些未知的错误,但是还不想直接抛出到上一级,那么就需要通过try…catch…的形式进行异常捕获,之后根据不同的异常情况来进行相应的处理。
-
抛出异常:你觉得无法解决的异常问题,且不需要调用者处理,那么你可以抛出异常。
throw关键字作用是在方法内部抛出一个
Throwable
类型的异常。任何Java代码都可以通过throw语句抛出异常。
Throws声明异常
子类声明异常的范围不能超过父类的声明异常
父类没有声明异常,子类也不能
自定义异常?
1.封装
2.快速定位
自定义异常步骤
1.创建类
2.继承exception类或者exception的子类
3.重写构造方法
手动抛出异常的步骤:
1.找到一个合适的异常类
2.创建这个异常类的对象
3.抛出这个对象
堆栈轨迹(stack trace):方法调用过程的列表!
自定义的异常
package com.liang.text.Exception;
/**
* 声明一个异常类,变胖啦
*/
public class FatException extends Exception{
public FatException(){//构造方法,无参构造
}
public FatException(String message){//构造方法,有参构造
super(message);
}
}
父类
package com.liang.text.Exception;
/**
* 父类
*/
public class Father {
public void eat() throws FatException{
}
}
子类
package com.liang.text.Exception;
/**
* 女儿
*/
public class Daughter extends Father{
public static void main(String[] args) {
Father babe = new Daughter();
try {
babe.eat();
}catch (FatException e){
e.printStackTrace();
}
}
@Override
public void eat() throws FatException {
System.out.println("吃炸鸡腿");
System.out.println("吃炸鸭腿");
System.out.println("吃炸猪腿");
throw new FatException("吃这么多会变肥的");//创建异常类对象,throw抛出异常
}
}
输出结果
吃炸鸡腿
吃炸鸭腿
吃炸猪腿
com.liang.text.Exception.FatException: 吃这么多会变肥的 //这个就是自定义异常类
at com.liang.text.Exception.Daughter.eat(Daughter.java:22)
at com.liang.text.Exception.Daughter.main(Daughter.java:10)
Process finished with exit code 0
for example
//导出
@RequiresPermissions("convergence:bzConvergence:view")
@RequestMapping(value = "export", method= RequestMethod.POST)
public String exportFile(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
try {
String fileName = "单位信息"+ DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
Page<BzConvergence> page = bzConvergenceService.findPage(new Page<BzConvergence>(request,response,-1),new BzConvergence());
new ExportExcel("单位信息", BzConvergence.class).setDataList(page.getList()).write(response, fileName).dispose();
return null;
} catch (Exception e) {
addMessage(redirectAttributes, "导出单位信息!失败信息:"+e.getMessage());
}
return "redirect:" + adminPath + "/business/convergence/bzConvergence/list?repage";
}