错误 Error 异常 Exception 识别 resolved
##异常
异常,不属于流程控制,专门门类,程序出现bug错误,可控错误。
错误:
1)致命错误:系统级别错误,蓝屏,磁盘坏了(不是程序员能解决的错误 Error)
2)程序BUG:一般逻辑错误。例如:扣钱 500(这些错误是程序员可控,这些错误叫异常 Exception)
常见的异常有什么呢?
1)10/0 a/b 被零除异常
2)数组下标越界 int[] a = new int[3],访问某个下标 a[30]
涉及到:try-catch,try-catch-finally,try-catch-throw,方法后main,单元测试方法后throws
try 试图
catch 捕获
finally 最终,释放资源
throw 抛出异常
throws 接收异常
package cn.tedu.exception;
import java.util.Scanner;
import org.junit.Test;
//演示异常处理,通过错误信息快速定位故障点
public class TestException {
@Test
public void tryCatch() {
//被零除异常
//try试图,没错尽量不要用
try {
int a = 10/0; // 把可能出错的代码放入
System.out.println(a);
}catch(Exception e) {
//捕获异常 //如果try中的代码出错,java会把错误信息封装到e对象实例中
e.printStackTrace();
//把错误信息打印到控制台上,用户看不到
}
}
@Test
public void tryCatchFinally() {
//键盘录入,造成非法值,异常
Scanner scan = new Scanner(System.in);
//从键盘上获取输入转换整数
System.out.println("请输入数字");
try {
int number = scan.nextInt();//把键盘输入的内容,回车后,转换成整数
System.out.println(number);
}catch(Exception e) {
e.printStackTrace();
}finally {//死活都执行,程序正常执行,程序出错还执行
scan.close();
System.out.println("执行finally");
}//Art+/快捷单词提示
}
@Test // 抛出异常,处理异常,数组越界
public void throwException() throws Exception{
try {
int[] n= {1,2,3};
System.out.println(n[3]);//越界报错
}catch(Exception e) {
e.printStackTrace();
//对异常进行处理,抛出到调用者(本方法),同时改变错误信息(中文),让调用者方便识别
//Unhandled exception type Exception //没人处理这个异常
throw new Exception("数组越界");//创建了一个新的异常,错误信息就变成参数的内容
//Unreachable code 不能到达的代码
//e.printStackTrace();
}
}
}
API的字符串String
java只所以强大,它有两个特点,jdk、平台生态链
1)jdk,java开发者工具包,它提供上万个基础类,方法,能方便调用计算机各个硬件处理
System.out 系统的输出设备(显示器);System.in 系统的输入设备(键盘)
开发效率就高,能实现各自业务需求
API application interface 应用程序接口(插排)。java怎么和计算机硬件通讯
2)平台生态链
JavaSE(jdk)、JavaEE(企业级开发、SSM、SpringCloud微服务)、JavaME(嵌入式)
package cn.tedu.string;
import java.util.Arrays;
import org.junit.Test;
//字符串常用方法
public class TestString {
@Test // 字符串拼接
public void concat() {
String s="chen";
String s1=s.concat("zishu");
System.out.println(s1);//chenzishu
// 两个字母:字母/斜杠,字母n,原样输出
String s2=s.concat("/n").concat("zishu");
System.out.println(s2);//chen/nzishu
// 第一个斜杠:转义字符,第二个斜杠真正斜杠
String s3=s.concat("\\n").concat("zishu");
System.out.println(s3);//chen\nzishu
// java表达换行符使用\n,ascii不可见,无法直接录入、
// 这里的斜杠\不是字符,而是转义字符标识
// 标识后面的n不是字母n,转换了一个意思
String s4=s.concat("\n").concat("zishu");
System.out.println(s4);//chen
//zishu
// tab键,\斜杠是转义字符,
String s5=s.concat("\t").concat("zizhou");
System.out.println(s5);//chen zizhou
}
@Test // 字符串格式化
public void furmat() {
String s = "a%sb%sc";// 使用%s隔开
/*
* format有n个参数, 第一个参数,字符串 第二个参数 Object... args 它叫做可变参数,参数个数不定 后面的参数个数和%s的个数一致
* 可变参数必须放在最后
*/
s = String.format(s,"100", "200");
System.out.println(s); //a100b100c
// 文件utf-8字符集:中文占1个,英文1个
// 早期字符集:中文占2个,英文1个,
// 车牌号7位,首字母中文
String s1 = "a"+"100"+"b"+"200"+"c";
System.out.println(s1); //a100b100c
System.out.println("字符串长度:"+s1.length());
//字符串长度:9
}
@Test // 去掉字符串两边的空格
public void trim() {
String s = " 陈 强 ";
System.out.println(s.trim());//陈 强
}
@Test
public void charAt() {
// 字符串底层字符数组
String name = "陈强是个好同志";
// 得到字符串的每个字母
System.out.println(name.charAt(1));//强
for(int i=0;i<name.length();i++) {
System.out.println(name.charAt(i));
//陈强是个好同志
}
}
@Test // 截取子串
public void substring() {
String name = "陈芙蓉";
String s1 = name.substring(0,1);
System.out.println(s1);//陈
String s2 = name.substring(1,3);
System.out.println(s2);//芙蓉
String s3 = name.substring(1);
System.out.println(s3);//芙蓉, 从1位置开始到最后
}
@Test
public void toCase() {
String name ="TonyChen";
System.out.println(name.toUpperCase());//TONYCHEN, 大写
System.out.println(name.toLowerCase());//tonychen,小写
// 根据类名获取到对象实例名称
// PersonMan personMan = new PersonMan();
// 截取字符串首字母,小写;和后面字符串拼接
String ClassName ="PersonMan";
// Type mismatch: cannot convert from char to String
// 任何类型和字符串拼接,结果都为字符串类型
String instanceName = (""+ClassName.charAt(0)).toLowerCase()
+ClassName.substring(1);
System.out.println(instanceName);//personMan
}
@Test
public void indexOf() {
String fileName ="gb.zhoujielun.mp3";
// lun子串在fileName字符串中出现位置
System.out.println(fileName.indexOf("lun"));//10
System.out.println(fileName.indexOf("."));//2
int pos = fileName.indexOf(".");
System.out.println(fileName.substring(pos+1,pos+4+1));//zhou
// 从右向左找到第一个.的位置
int posend = fileName.lastIndexOf(".");
String extName = fileName.substring(posend);
System.out.println(extName);//.mp3
}
@Test
public void with() {
String name = "teduChen.mp4";
String prefix = "tedu";// 前缀
String suffix = ".mp4";// 后缀
System.out.println(name.startsWith(prefix));//true
System.out.println(name.endsWith(suffix));//true
}
@Test //替换
public void replace() {
String name = "陈强 ";
//把名字中的强字替换*
System.out.println(name.replace("强", "*"));//陈*
String fileName = "chen.mp4";
//把点替换*
System.out.println(fileName.replace(".", "*"));//chen*mp4
/*
* replaceAll的第一个参数regex 正则表达式
* 这个.是正则表达式的特殊符号,它代表匹配每个字符
* 第一个斜杠转义,第二个斜杠字符
*/
System.out.println(fileName.replaceAll("\\.","*"));//chen*mp4
}
@Test
public void split() {
//使用,隔开:分隔符
String name = "陈强,王琪,李斯";
//需要它的每个值
String[] s = name.split(",");//拆串
System.out.println(s);//打印内存地址
System.out.println(Arrays.toString(s));//[陈强, 王琪, 李斯]
String file = "a.b.c.d";
String[] f = file.split("\\.");
System.out.println(Arrays.toString(f));//[a, b, c, d]
}
}