boy532,夏洛特的烦恼迅雷下载,至尊食王片尾曲
throw抛出异常的方式比较直接:
if(age < 0){ throw new myexception("年龄不能为负数!"); }
来看一个例子:
package test; public class test2 { public static void main(string[] args) { string s = "abc"; if(s.equals("abc")) { throw new numberformatexception(); } else { system.out.println(s); } } }
运行结果如下:
java中可以对一个方法在定义时就进行异常的声明,而后在实现时可以利用throw具体的抛出异常。
ppublic class shoot { 创建类 static void pop() throws negativearraysizeexception { //定义方法并抛出negativearraysizeexception异常 int [] arr = new int[-3];//创建数组 } public static void main(string[] args) {//主方法 try { pop(); //调用pop()方法 } catch (negativearraysizeexception e) { system.out.println("pop()方法抛出的异常");//输出异常信息 } } }
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
浅析我对 String、StringBuilder、StringBuffer 的理解
使用IDEA搭建SSM框架的详细教程(spring + springMVC +MyBatis)
Springboot整合freemarker 404问题解决方案
引入mybatis-plus报 Invalid bound statement错误问题的解决方法
网友评论