2019.2.20异常
Posted zhangchuanfeng1
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2019.2.20异常相关的知识,希望对你有一定的参考价值。
1.
package qq; import java.util.Scanner; public class KeCheng { public static void main(String[] args) { Scanner input = new Scanner(System.in); try { System.out.println("请输入1~3之间的数字:"); int choose = input.nextInt(); switch (choose) { case 1: System.out.println("JAVA"); break; case 2: System.out.println("C++"); break; case 3: System.out.println(".net"); break; default: System.out.println("输入错误!没有该课程"); break; } } catch (Exception e) { e.printStackTrace(); System.err.println("出现异常!"); }finally { System.out.println("感谢建议"); } } }
输出为:
2.
package qq; /** * 年龄类 */ public class Age { private int age; public int getAge() { return age; } public void setAge(int age) throws Exception{ if(age<100&&age>0) { this.age = age; }else { throw new Exception("年龄不能小于0或者大于100"); } } public void print() { System.out.println("XXX的年龄为:"+this.getAge()); } }
package qq;
/**
* 年龄测试类
*/
import java.util.Scanner;
/**
* 年龄测试类
*/
import java.util.Scanner;
public class AgeTest {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
try {
System.out.println("请输入年龄:");
Age age = new Age();
age.setAge(input.nextInt());
age.print();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
try {
System.out.println("请输入年龄:");
Age age = new Age();
age.setAge(input.nextInt());
age.print();
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出为:
先执行finally块,在执行return
System.exit(1);退出虚拟机
以上是关于2019.2.20异常的主要内容,如果未能解决你的问题,请参考以下文章
mvn命令异常:An error has occurred in Javadoc report generation: Unable to find javadoc command异常已解决(代码片段
mvn命令异常:An error has occurred in Javadoc report generation: Unable to find javadoc command异常已解决(代码片段
springcloud报错-------关于 hystrix 的异常 FallbackDefinitionException:fallback method wasn't found(代码片段