enum的创建(类似类和接口,枚举也是一种引用数据类型)
//枚举的创建
public enum Season {
SPRING,SUMMER,AUTUMN,WINTER
}
public class EnumTest {
public static void main(String[] args) {
System.out.println(Season.SPRING);
System.out.println(Season.SUMMER);
System.out.println(Season.AUTUMN);
System.out.println(Season.WINTER);
}
}
//这里我把Season中的内容改成了SUCCESS、FAIL
public class EnumTest {
public static void main(String[] args) {
System.out.println(divide(10,0)==Season.SUCCESS?"计算成功":"计算失败");
//if (hello() == Season.SPRING) System.out.println("春天!");
}
public static Season divide(int a,int b){
try {
int c = a / b;
return Season.SUCCESS;
}catch (Exception e){
return Season.FAIL;
}
}
}
switch支持int、String、enum类型。
switch(enum.param);
异常类:
Exception 有子类Throwable,表示可抛出的
可抛出子类包括Error和Exception,都是可抛出的。
Error表示不可处理,Exception表示可以处理。
编译时异常是指在编写代码时必须对这种异常进行处理,否则编译报错。
运行时异常在编译时可以选择处理,也可以选择不处理。例如除法的除数不能为0,而输出除数的值是在运行时由操作人员进行操作输入的,只能抛出或者提供一个处理模块,因其在程序运行的过程中出现,所以称为运行时日常。
编译时异常又称受检、受控异常,运行时异常常也称为非受控或未受检异常,所有异常都发生在运行阶段。
异常处理方式有两种:
第一:在方法生命时使用throws Exception将异常抛给上一级。 (谁调用方法,异常抛给谁)
public static void main(String[] args) throws ClassNotFoundException{ //上抛处理异常
method();
}
public void method() throws ClassNotFoundException{
}
第二:使用try......catch语句进行异常捕获
public static void main(String[] args){
try{
method();
}catch(ClassNotFoundException e){
//作用
e.printStackTrace();
}
}
public void method() throws ClassNotFoundException{
}
try是可能出现异常的代码体,catch是捕捉的意思,即捕捉到错误之后执行的代码体。
throws 可以同时抛出多个异常,每个异常通过逗号隔开。
try体中如果当前句发生异常,则try体中后面的语句则不再执行。
异常也是类,故也支持多态。
catch捕捉的异常应该具体,FileNotFoundException、IOException等具体异常。
jdk8 支持catch(E1 | E2 | E3) //括号里应填写详细异常类名,以下除非特别情况外统一以E代替详细类名
E e = new E("asdadsad");
e.getMessage(); //打印出asdadsad
e.printStackTrace(); //报错。实际开发中推荐使用
异常信息只需要看自己的代码就行,从上往下修改。
finally是必定执行的,必须与try一起出现,适用于资源释放、关闭。
try{}catch{}finally{}
try{
........
return;
}finally{
sout("finally");
} //执行结果为 ......后finally后return
//特别的:
int i = 100;
try{
return i;
}finally{
i++;
}
//此时出现return回来的i为100!!
//try finally语句块的代码除非在当前代码块输出,否则值是不会变的。
异常在实际开发中的作用
package ExceptionTest;
import java.util.Scanner;
/*
输入账号密码,后台模拟注册账户
要求用户名长度 6~14 小于或大于都抛出异常
注册类 + 异常类
*/
public class ExceptionWork {
public static void main(String[] args) {
System.out.println("Please enter your username & password!");
Scanner s = new Scanner(System.in);
String usn = s.next();
System.out.println(usn.length());
String psw = s.next();
UserService u = new UserService();
try {
u.register(usn,psw);
u.register("12340","1234123");
} catch (RegisterException e) {
System.out.println(e.getMessage());
}
}
}
class UserService{
public void register(String usn,String psw) throws RegisterException {
/**
* 用户注册
* 先判定用户名位数是否符合,配合异常机制,小于抛出位数小于异常,大于抛出位数大于异常
*/
if (null == usn || usn.toCharArray().length < 6 ||usn.toCharArray().length > 14){
throw new RegisterException("用户名小于或大于14位,不符要去,请重新输入!");
// RegisterException r = new RegisterException("用户名小于6位或大于14位,不符要求,请重新输入!");
// System.out.println(r.getMessage());
}
System.out.println("恭喜用户" + usn + "您注册成功!");
}
}
class RegisterException extends Exception{
public RegisterException() {
super();
}
public RegisterException(String s){
super(s);
}
}
异常在重写时的注意事项
异常在重写时不应该比原类抛出更多异常。如果在原方法中没有抛出异常,但是在重写的方法中却可以抛出RuntimeException。 //why?