Java基础05

异常

在这里插入图片描述
Java异常处理是通过五个关键字try、catch、finally、throw、throws。
try-catch-finally:无论是否发生异常,finlly总会执行,即使有return也会执行。
try-catch-catch:多重catch,捕捉具体异常

try

其他用法

try 语句用于捕获和处理可能发生的异常。这里的 try 语句后面跟着一对圆括号 ( 和 ),表示这是一个资源管理的 try-with-resources 语句。这种语法确保在 try 块执行完毕后,自动关闭所有在 try 语句中声明的资源。

 try (
                FileInputStream fis = new FileInputStream(filePath);
                FileOutputStream fos = new FileOutputStream(filePath + ".tmp") // 写入临时文件
        ) {
            int num = fis.available();
            byte[] b = new byte[num];
            fis.read(b, 0, num);
            String str = new String(b, StandardCharsets.UTF_8); // 指定字符集

            str = str.replace("{name}", "小明")
                    .replace("{age}", "18")
                    .replace("{id}", "1001");

            byte[] newBytes = str.getBytes(StandardCharsets.UTF_8); // 使用相同的字符集编码
            fos.write(newBytes);
        }

throws声明异常,调用者不打算处理异常则通过throws声明异常,让上一级调用者处理。当声明(抛出)的异常是运行时异常调用者不强制处理。声明(抛出)的异常是编译时异常时调用者需要强制处理。

public ststic int m () throws Exception{
    sout("我可能会出现异常");
}
//抛出不同异常调用者处理方式
public class Throws {
    public void m () throws NullPointerException {
        System out println("我是运行时异常不需要强制处理m ()");
    }
    public void m () throws Exception {
        System out println("我是编译时异常需要强制处理m ()");
    }

    public static void main(String[] args) {
        Throws throwsTest = new Throws();
        throwsTest m ();//不需要强制处理
        try {
            throwsTest m ();//需要强制处理
        }catch (Exception e) {
            System out println("处理异常");
        }
    }

}

throw 抛出异常

public class Throw {
    public void m1(int a, int b) throws Exception{
        if (a==b){
            System out println("a等于b");
        }else {
            throw new Exception("a需要等于b");
        }
        System out println("m1");
    }

    public static void main(String[] args) {
        Throw t = new Throw();
        try {
            t.m1( 1, 1);
    }catch (Exception e){
            e printStackTrace();//输出异常堆栈信息
            System out println(e.getMessage());// 输出异常信息
        }
    }
}

自定义异常

public class MyException extends Exception {
    String message;
    public MyException(String message) {
        super(message);
    }
public static void m1() throws MyException {
    throw new MyException("这是我的异常信息");
    }
public static void main(String[] args) {
    try {
        m1();
    } catch (MyException e) {
        System out println(e.getMessage());
    }
    }
}

Git

在这里插入图片描述

![外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传](https://img-home.csdnimg.cn/images/20230724024159.png?origin_url=photo%2F%2FGit.jpg&pos_id=img-Zwjz2DRk-1725525041425

基本配置

配置用户名和邮箱
git config --global user name “hjg”
git config --global user email “hjg@hzlx com”
查看配置
git config --global user name
git config --global user email

新建本地仓库

打开diea,目录选择vcs,选择Create Git Repository,选择要创建的文件即可

修改过的代码往本地仓库提交

打开idea,目录选择git,然后选择commit,绿色的文件就能提交

本地仓库提交到远程仓库

将提交到本地文件的代码push,填写远程仓库的地址,点击ok,然后输入码云账户,点击push。

删除本地仓库

在文件目录里找到 git文件删除,再打开idea,setting选择Version Control下的Dirctory,解除绑定即可。

第一次从git远程仓库下载代码

登录git账户,选择下载,复制文件地址,在桌面新建文件夹,文件夹里右键打开git命令行,输入git clone 刚复制的地址。

开发过程中从远程仓库拉代码

idea里选择pull即可!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值