4.第三单元任务三实训:编写一个类ExceptionTest,在main方法中使用try-catch-finally语句结构实现:在try语句块中,编写两个数相除操作,相除的两个操作数要求程序运行时用
2015-07-21 18:24
1586 查看
4.第三单元任务三实训:编写一个类ExceptionTest,在main方法中使用try-catch-finally语句结构实现:在try语句块中,编写两个数相除操作,相除的两个操作数要求程序运行时用户输入;在catch语句块中,捕获被0除所产生的异常,并输出异常信息;在finally语句块中,输出语句"异常处理"。
import java.util.Scanner;
public class ExceptionTest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入除数:");
int op1 = input.nextInt();
System.out.println("请输入被除数:");
int op2 = input.nextInt();
int result = 0;
try {
result = op1 / op2;
} catch (ArithmeticException e) {
e.printStackTrace();
} finally {
System.out.println("异常处理");
}
System.out.println(result);
}
}
import java.util.Scanner;
public class ExceptionTest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入除数:");
int op1 = input.nextInt();
System.out.println("请输入被除数:");
int op2 = input.nextInt();
int result = 0;
try {
result = op1 / op2;
} catch (ArithmeticException e) {
e.printStackTrace();
} finally {
System.out.println("异常处理");
}
System.out.println(result);
}
}
相关文章推荐
- rails 中使用fusionchart的图表导出功能
- Quartz 2.2.1学习笔记 (二) Jobs、JobDetail、JobDataMap
- 2015 Multi-University Training Contest 1
- Failed to fetch URL http://dl-ssl.google.com************
- hdu 1023 Train Problem II (卡特兰数)
- HDU 2473Junk-Mail Filter 【并查集,删点】
- 注册表-在IE上永久显示我的名字"www.baidu.com - 朱建强"
- Email正则匹配
- Leetcode #70 Climbing Stairs
- 【算法渣渣的逆袭之路】Property Distribution Aizu - 0118
- int main(int argc,char* argv[])详解
- HDOJ Air Raid 1151【最小路径覆盖】
- LEAR's main research areas are:
- fail2ban防暴力破解
- 【学习过程】Pair
- JAX-WS HandlerChain使用详解
- mdadm创建software RAID
- hdu 1022 Train Problem I
- Git修改提交的用户名和Email
- [XCode错误]XCode编译出错异常:ld: framework not found Kernel clang: error: linker command failed with e