package hello;
//自定义异常
class MathException extends Exception{
MathException(){
System.out.println("输入的格式不正确!");
}
}
public class Hello{
public static void input(String t) throws MathException //抛出异常选项
{
int i;
char[]stringArr=t.toCharArray(); //将自字符串放进数组
for(i=0;i<t.length();i++) { //通过数组遍历,检验字符串是否由纯数组组成
if(stringArr[i]<49||stringArr[i]>57) {
throw new MathException(); //抛出异常
}
}
System.out.println(String.valueOf(t));
}
public static void main(String args[]) {
try {
String t=new String("123123");
input(t);
}
catch(MathException e) {
System.out.println(e);
}
finally {
System.out.println("程序运行结束!");
}
}
}```
java二级-捕获异常
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 编写一个方法,比较两个字符串。假如其中一个字符串为空,会产生NullPointerException异常,在方法声...
- 本博客为个人原创,转载需在明显位置注明出处 这篇文章我们来聊聊捕获异常的内容,如果一个方法内部抛出两个或多个异常,...
- 编写应用程序,从命令行传入两个整型数作为除数和被除数。要求程序中捕获NumberFormatException 异...
- 当线程出现未捕获异常(即抛出一个异常) JVM将调用Thread.dispatchUncaughtExceptio...