标签:checked nfa extend this except font back 字符串 cat
异常:
语法:
try{ //代码段(此处不会出现异常) }catch(异常Exception或Exception的子类){ //对异常进行处理的代码段 } //代码段 } public class Test1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("请输入被除数:"); // 除数为0异常: ArithmeticException // 输入格式不正确: InputMismathException(子类) try{ //程序中可能出现异常的代码 int num1 = in.nextInt(); System.out.print("请输入除数:"); int num2 = in.nextInt(); System.out.println(num1+"/"+ num2 +"="+ num1/ num2); }catch(ArithmeticException e){ //在程序出现异常的情况下执行的代码 System.err.print("出现错误操作"+"\n"); //异常对象 e printStackTrace();打印异常堆栈信息 e.printStackTrace(); } System.out.println("感谢使用本程序!"); } }
tyr–cetch–finally语法:
try{ //代码块 }cetch(){ //代码块 }finally{ //代码块 } public class Test2 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("请输入被除数:"); // 除数为0异常: ArithmeticException // 输入格式不正确: InputMismathException(子类) try { //程序中可能出现异常的代码 int num1 = in.nextInt(); System.out.print("请输入除数:"); int num2 = in.nextInt(); System.out.println(num1 + "/" + num2 + "=" + num1 / num2); } catch (ArithmeticException e) { //在程序出现异常的情况下执行的代码 System.err.print("出现错误操作" + "\n"); // 异常对象 e printStackTrace();打印异常堆栈信息 e.printStackTrace(); } finally { System.out.println("感谢使用本程序!"); } } }
多重cetch块:
try{ //代码块 }cetch(){ //代码块 }cetch(){ //代码块 }cetch(){ //代码块 }finally{ //代码块 } public class Test3 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("请输入被除数:"); // 除数为0异常: ArithmeticException // 输入格式不正确: InputMismatchException(子类) try { int num1 = in.nextInt(); System.out.print("请输入除数:"); int num2 = in.nextInt(); System.out.println(num1 + "/" + num2 + "=" + num1 / num2); } catch (ArithmeticException e) { System.err.print("除数不能为0" + "\n"); // 异常对象 e printStackTrace();打印异常堆栈信息 e.printStackTrace(); } catch (InputMismatchException e) { System.err.print("输入格式不正确" + "\n"); e.printStackTrace(); } catch (Exception e) { System.err.print("未知异常" + "\n"); e.printStackTrace(); }finally { System.out.println("感谢使用本程序!"); } }
约束条件:越具体的子异常类放在catch首位,越抽象的父异常类越往后写
cetch语句顺序:先子类,后父类
发生异常时按顺序逐个匹配
只执行第一个与异常类型匹配的cetch语句
try块为必须,但不能单独出现。
throws :
public void 方法名()throws 异常类型{ //代码块 } public static void main(String[] args){ 类名 t = new 类名(); try{ t.方法名(); } }
public static void a() throws ArithmeticException{ int num=5/0; throw new ArithmeticException("除数不能为0"); } public static void b(){ try{ a(); }catch(ArithmeticException e){ e.printStackTrace(); } }
或者继续抛出:
public void 方法名()throws 异常类型{ //代码块 } public static void main(String[] args) throws 异常类型{ 类名 t = new 类名(); t.方法名(); } public static void main(String[] args) { try{ b(); }catch(ArithmeticException e){ e.printStackTrace(); } } public static void a() throws ArithmeticException{ int num=5/0; throw new ArithmeticException("除数不能为0"); } public static void b() throws ArithmeticException{ a(); }
throw:
public void 方法名()throws Exception{ throw new Exception(“性别只能为男女”); } throw new ArithmeticException("除数不能为0");
throw必须处理或者继续抛出异常。
常见异常类型:
public class LoginFaildException extends Exception { public LoginFaildException() { super(); } public LoginFaildException(String message, Throwable cause) { super(message, cause); } public LoginFaildException(String message) { super(message); } public LoginFaildException(Throwable cause) { super(cause); }
public class User { private String userName; private String passWord; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; }
// 注册方法 public static boolean register(User user) throws RegisterException { //声明一个返回值为布尔类型的方法 boolean flag = false; //声明返回值变量 if (user.getUserName() == null) { if (!flag) { throw new RegisterException("用户名不能为空,注册失败"); //抛出注册异常 } else { return true; } }else if(user.getPassWord().equals(null)){ return false; } return true; } // 登录方法 public static void login(User user) throws LoginFaildException { try { boolean register= register(user); } catch (RegisterException e) { e.printStackTrace(); throw new LoginFaildException("登录失败",e); //核心代码,将注册与登录异常抛给登录异常构造方法 } }
public static void main(String[] args) { User user = new User(); try { login(user); } catch (LoginFaildException e) { e.printStackTrace(); //抛出异常 }
exception.RegisterException: 用户名不能为空,注册失败 at test.Test4.register(Test4.java:23) at test.Test4.login(Test4.java:36) at test.Test4.main(Test4.java:12) exception.LoginFaildException: 登录失败 at test.Test4.login(Test4.java:40) at test.Test4.main(Test4.java:12) Caused by: exception.RegisterException: 用户名不能为空,注册失败 at test.Test4.register(Test4.java:23) at test.Test4.login(Test4.java:36) ... 1 more
标签:checked nfa extend this except font back 字符串 cat
原文地址:https://www.cnblogs.com/big-data-sky/p/10981726.html