Java异常
2021/11/13 22:41:36
本文主要是介绍Java异常,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Exception
- 检查性异常 「测试」
- 运行时异常
- 错误ERROR
异常处理框架
异常处理机制
- 抛出异常
- 捕获异常
public class Test { public static void main(String[] args) { int a = 1; int b = 0; try { //try监控区域 System.out.println(a / b); } catch (ArithmeticException e){ //catch捕获区域 // 括号内为捕获异常的属性 最高为Throwable System.out.println("程序出现异常"); }finally{ //无论是否出现异常,都执行 System.out.println("finnaly"); } } }
command+option+t 可以自动生成try/catch
e.printStackTrace();
打印错误栈信息
主动抛出异常 throw/throws
方法抛出异常,一般处理不了
if(b==0){ throw new ArithmeticException(); //主动抛出异常,一般在方法中使用 }
方法上抛出异常
public void test(int a, int b) throws ArithmeticException{ ... }
自定义异常(少用)
继承exception
类
public class MyException extends Exception{ ... }
这篇关于Java异常的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-26结对编程到底难不难?答案在这里
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)