java异常Exception

五大异常关键字

  1. Exception,分为检查性异常、运行时异常和错误Error(如栈溢出等,如outofMemoryError,JVM一般是终止线程);
  2. java中有多种异常,他们的超级异常是java.lang.Throwable;
  3. try-catch-finally---三个关键字,快捷键:对执行代码Ctrl+Alt+T
    如图所示:
java 复制代码
package oopDemo.test03;

public class TestExc {
    static void main(String[] args) {
        int a=10;
        int b=0;
        try {
            System.out.println(a/b);//1、尝试执行代码:可能抛出异常的代码块
        } catch (ArithmeticException e) {
            System.out.println("代码异常了,除数为0了");//2、捕获并处理异常:当try块中发生指定类型的异常时执行
        } finally {
            System.out.println("一切结束ok");//3、最终执行的代码:无论是否发生异常,都会执行的代码块
        }
    }
}
  1. throw -throws:一个用在方法体中throw,一个用在方法名后面throws;
java 复制代码
package oopDemo.test02;

public class Test02 {
    static void main(String[] args) {
            new Test02().test(2,0);

    }
    public void test(int a, int b)throws ArithmeticException{
        if(b==0){
            throw new ArithmeticException();
        }
    }
}
相关推荐
Chester_19995 小时前
CSP202203C.计算资源调度器
开发语言·数据结构·c++·蓝桥杯
EXI-小洲7 小时前
Java 操作 Word:字符串替换、图片插入、动态生成表格与API接口下载
java·开发语言·spring boot·word
会周易的程序员7 小时前
给 PLC 写一个字节码虚拟机:STVM 虚拟机架构设计
开发语言·c++·虚拟机·软plc·iec61131·stvm
机器视觉知识推荐、就业指导7 小时前
为什么老说“纯 Qt 没啥就业市场”?
开发语言·qt
telepan7 小时前
Qt 开发避坑与性能指南:如何优雅、安全地定义全局常量字符串?
开发语言·qt
m0_695251497 小时前
Qt MaintenanceTool 使用国内镜像加速下载(超详细教程)
开发语言·qt
2601_961901708 小时前
SpringBoot使用Nacos进行application.yml配置管理
java·spring boot·spring
何以解忧,唯有..8 小时前
LangChain 工具调用(Tool Calling)实战指南
java·前端·langchain
大衛說8 小时前
《Python 从入门到精通》系列总览与学习路线
开发语言·python·学习
QQ_21696290969 小时前
【源码编号:project79475】SpringBoot校内二手交易平台:商品发布、分类检索、留言交流、订单管理全流程实战
java·spring boot·后端