2021-06-10

1 篇文章 0 订阅
1 篇文章 0 订阅

day16

除数不为0,异常解决

public class AssertTest {
    public static void main(String[] args) {
        AssertTest assertTest = new AssertTest();
        assertTest.test(0);
    }
    
    public void test(int a) {
        assert a != 0 : "参数不能为0";
        int c = 10 / a ;
        System.out.println(c);
        System.out.println("hello");
    }
}

异常处理方法

1.  throws Exception
2.  try {   //可能会发生异常的代码
            throwTest.test();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

Login

import java.util.HashMap;
import java.util.Map;
​
public class LoginTest {
    Map<String, User> map;
    {
        map = new HashMap<String, User>();
        User user = new User("tom", "123");
        map.put("tom",user);
    }
    
    public void login(String username,String password) throws LoginException {
        if (!map.containsKey(username)) {
            throw new LoginException("用户名不正确");
        }else {
            System.out.println("用户名正确");
        }
        User user = map.get(username);
        if (!user.password.equals(password)) {
            throw new LoginException("密码错误");
        }else {
            System.out.println("密码正确");
        }
    }
    public static void main(String[] args) {
        LoginTest loginTest = new LoginTest();
        try {
            loginTest.login("tom", "123");
        } catch (LoginException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
public class LoginException extends Exception{
    
    public LoginException(String massage) {
        super(massage);
    }
    public LoginException() {
        // TODO Auto-generated constructor stub
    }
}
​

线程创建方法

public class MyThread extends Thread{
    @Override
    public void run() {
        System.out.println(MyThread.currentThread());
        for (int i = 0; i < 10; i++) {
            System.out.println("helloworld");
        }
    }
}
Thread t1 =new Thread() {
            public void run() {
                for (int i = 0; i < 100; i++) {
                    System.out.println("hello" + i);
                }
            }
        };
Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                System.out.println(Thread.currentThread());
                //System.out.println("t2的线程");
            }
        },"t2线程");

定义Thread的Name

1. Thread xiaoli = new Thread(target,"xiaoli");
2. Thread jack = new Thread(target);
        jack.setName("jack");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值