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...