8月30日学习笔记

课程大纲
  1. 异常处理
  2. String和Date类学习
课程笔记

一、异常处理
1.概括:运行中层序中断JVM抛出异常,程序会中断;
2.两种解决方案①try catch ②throw抛出异常
①try catch

public class Client1Exception {
	public static void main(String[] args) {
		System.out.println(fun1(3,0));
		System.out.println("111");
	}
	public static int fun1(int x, int y) {
		int rs = 0;
		try {
			rs = x / y;
		} catch(Exception e) {
			e.printStackTrace();
			System.out.println("出错");
		}
		return rs;
	}
}

这里写图片描述
②throw

public class Client1 {
	public static void main(String[] args) throws Exception {
		fun2(3);
		System.out.println("111");
	}
	public static void fun1(int age) throws Exception {
		if(age <10) {
			throw new Exception("异常");
		}
	}
	public static void fun2(int age) throws Exception {
		try {
			fun1(age);
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
}


java.lang.Exception: 异常
	at com.dodoke.d0830.Client1.fun1(Client1.java:10)
	at com.dodoke.d0830.Client1.fun2(Client1.java:15)
	at com.dodoke.d0830.Client1.main(Client1.java:5)
111

3.自定义异常(业务异常)
继承Exception类,调用父类构造器

public class E1 extends Exception{
	/**
	 * 自定义异常
	 */
	private static final long serialVersionUID = 1L;

	public E1(String msg) {
		super(msg);
		System.out.println("数字不允许");
	}
}

4.常见异常
NullPointerException 在调用对象属性或者方法的时候,对象为 null,报异常;
java.lang.ArrayIndexOutOfBoundsException:数组越界
java.lang.IndexOutOfBoundsException:集合越界

二、String和Date类使用—API
学习类从三个方面入手:属性、方法、构造器;
String—extends Object;

public class Index {
	public static void main(String[] args) {
		//char[] str={'d','o','d','o','k','e',};
		String str="dodoke";
		//charAt索引字符串位置
		//char a =str.charAt(2);
		//System.out.println(a);
		//在末尾添加字符串
		//String b=str.concat("真棒!")
		//contains判断字符串是否有元素,返回值为Boolean
		//Boolean c=str.contains("o");
		//endsWith判断字符串结尾是否为制定值
		//Boolean d=str.endsWith("c");
		//Boolean e=str.endsWith("e");
		//equals比较两个字符串是否相等
		//Boolean f=str.equals("dodoke");
		//Boolean g=str.equals("dodok");
		//!!!String h=str.format("%c","c");
		//indexOf 索引
		//int i = str.indexOf("d",1);
		//int j=str.indexOf("o");
		//判断是否为空
		Boolean k=str.isEmpty();
		//计算字符串长度
		int l=str.length();
		//替换,先写old,再写添加的元素
		//使用指定的字面值替换序列   替换   此字符串所有匹配   字面值目标序列   的子字符串。
		String m=str.replace("d","D");
		String n=str.replace("o","你是认真的吗?");
		 //!!!使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
		 //String[] h=str.split();
		 //判断首字符是否为指定字符
		 Boolean t=str.startsWith("a");
		 String x=str.substring(1);
		 //substring(1,3)取不到3
		 String y=str.substring(1,3);
		 String z=str.toUpperCase();
		 String z1=str.trim();
		System.out.println(z1);
	}
}

Date java-util

Date d = new Date(); // 获取当前的服务器时间

获取年、月、日、时、分、秒
getYear
getMonth
getDate
getHours
getMinutes
getSeconds
**SimpleDateFormat-----extends java.text.DateFormat **
格式化时间

Date date = new Date();
SimpleDateFormat s = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss.SSS");
System.out.println(s.format(date));
//控制台打印
2018年08月30日 20:47:16.759

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值