可以抛出的。Throwable类是所有异常的顶级父类,所有的异常都具有可抛性。在Java中,able结尾的,一般表示某种能力:
运行时异常
检测时异常 编译时异常 非运行时异常
抛出异常,扔给调用者来处理。throws并没有真正处理异常,只是把异常往外抛出了,知识骗过了编译器。(消极的异常处理方式)main方法不建议throws。
捕获异常。
public class TestCatch {
public static void main(String[] args) {
int a= 2;
int b= 0;
try{// 捕获可能有异常的语句
System.out.println(a/b);
}catch(ArithmeticException e){
System.out.println("ArithmeticException
}
}
}
public class TestCatch {
public static void main(String[] args) {
int a= 2;
int b= 0;
String str = new String("av");
String s = "abc";
try{
System.out.println(a/b);
System.out.println(str.charAt(0));
int ni = Integer.parseInt(s);
// RuntimeException是父类,所以将拦截所有异常,下面的两个就执行不到
}catch(RuntimeException e){
System.out.println("ArithmeticException ");
}catch(NullPointerException e){
System.out.println("NullPointerException");
}catch(NumberFormatException e){
System.out.println("NumberFormatException");
}
}
}
public class TestFinally {
public static void main(String[] args) {
System.out.println("return:"+m1());// return:3
}
public static int m1(){
int a = 2;
int b = 1;
// finally无论如何都会执行,程序流程为:1>5>6 如果将b改为0:1>3>5>6
try {
System.out.println(a/b);// 1
return 1;// 2
} catch(Exception e){
System.out.println(e.toString());// 3
return 2;// 4
} finally{
System.out.println("finally");// 5
return 3;// 6
}
}
}
throw+异常对象
将该异常手动抛出。public class DrinkNotFoundException extends Exception{// 自定义饮品没有找到异常
public DrinkNotFoundException(String msg){
super(msg);
}
}
// 使用自定义异常
public abstract class Drink {
public final int COFFEE = 1;// 饮品编号
public final int BEER = 2;
public final int MILK = 3;
public abstract void taste();
public static Drink getDrink(int flag)throws DrinkNotFoundException{// 静态工厂返回饮品实例,编译时异常所以要抛出
switch(flag){
case 1:
return new Coffee();
case 2:
return new Beer();
case 3:
return new Milk();
default:
// 抛出饮品没有找到异常
throw new DrinkNotFoundException("对不起,没有您输入的饮品类型!");
}
}
}
// 调用getDrink()方法
public class Test {
public static void main(String[] args) {
try{
Drink a = Drink.getDrink(4);// 4是没有的饮品编号,将抛出异常
a.taste();// 打印饮品口味
}catch(DrinkNotFoundException e){
e.printStackTrace();// 打印异常堆栈跟踪信息
}
System.out.println("else");
}
}
String msg = "1001,张三,man";
String[] strArr = msg.split(",");
System.out.println(strArr[0]);// 1001
System.out.println(strArr[1]);// 张三
System.out.println(strArr[2]);// man
String ip = "192.168.3.18";
// split:按照正则表达式
// 如果拆分不出来,考虑转义
String[] ipArr = ip.split("\\.");
System.out.println(Arrays.toString(ipArr));
String s1 = "abcabcabc";
String s2 = s1.replace("a", "中");
System.out.println(s2);// 中bc中bc中bc
// replaceAll支持正则表达式
String s3 = s1.replaceAll("a", "中");
System.out.println(s3);// 中bc中bc中bc
System.out.println("abc".contains("a"));// true
String s4 = "abcd";
char[] chArr = s4.toCharArray();
System.out.println(Arrays.toString(chArr));//[a, b, c, d]
System.out.println("AbCd".toLowerCase());// abcd
System.out.println("abcd".toUpperCase());// ABCD
StringBuilder
& StringBuffer
:用法相同
StringBuilder builder = new StringBuilder();
builder.append("abc");
builder.append(1.0);
builder.append(true);
builder.append(20).append("def").append("123");
System.out.println(builder);// abc1.0true20def123
// StringBuilder 转为String
String result = builder.toString();
System.out.println(result);// abc1.0true20def123
BigDecimal bd1 = new BigDecimal("2.6");
BigDecimal bd2 = new BigDecimal("2.0");
double d = bd1.subtract(bd2).doubleValue();
System.out.println(d);// 0.6 如果使用double类型则是0.600...01
System.out.println(Double.MAX_VALUE);// double型最大值
BigDecimal bd3 = new BigDecimal(Double.MAX_VALUE);
BigDecimal bd4 = new BigDecimal(Double.MAX_VALUE);
System.out.println(bd3.add(bd4));// 相加