java异常处理课后作
1、动手动脑
源码
public static void main(String[] a)
{
int i=1, j=0, k;
k=i/j;
{
k = i/j; // Causes division-by-zero exception
//throw new Exception("Hello.Exception!");
}
catch ( ArithmeticException e)
{
System.out.println("被0除. "+ e.getMessage());
}
catch (Exception e)
{
if (e instanceof ArithmeticException)
System.out.println("被0除");
else
{
System.out.println(e.getMessage());
}
}
finally
{
JOptionPane.showConfirmDialog(null,"OK");
}
}
java把可能会发生错误的代码放进try语句块中,catch语句块中的代码用于处理错误,不管是否有异常发生,finally语句块中的语句始终保证被执行。如果没有提供合适的异常处理代码,JVM会结束掉整个应用程序。
2、动手动脑
源码
public class CatchWho {
public static void main(String[] args) {
try {
try {
throw new ArrayIndexOutOfBoundsException();
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch");
}
throw new ArithmeticException();
}
catch(ArithmeticException e) {
System.out.println("发生ArithmeticException");
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");
}
}
}
结果
ArrayIndexOutOfBoundsException/内层try-catch
发生ArithmeticException
源码
public class CatchWho2 {
public static void main(String[] args) {
try {
try {
throw new ArrayIndexOutOfBoundsException();
}
catch(ArithmeticException e) {
System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch");
}
throw new ArithmeticException();
}
catch(ArithmeticException e) {
System.out.println("发生ArithmeticException");
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");
}
}
}
结果
ArrayIndexOutOfBoundsException/外层try-catch
3、动手动脑
源码
public static void main(String args[]) {
int result;
try {
System.out.println("in Level 1");
try {
System.out.println("in Level 2");
// result=100/0; //Level 2
try {
System.out.println("in Level 3");
result=100/0; //Level 3
}
catch (Exception e) {
System.out.println("Level 3:" + e.getClass().toString());
}
finally {
System.out.println("In Level 3 finally");
}
// result=100/0; //Level 2
}
catch (Exception e) {
System.out.println("Level 2:" + e.getClass().toString());
}
finally {
System.out.println("In Level 2 finally");
}
// result = 100 / 0; //level 1
}
catch (Exception e) {
System.out.println("Level 1:" + e.getClass().toString());
}
finally {
. System.out.println("In Level 1 finally");
}
}
in Level 2
in Level 3
Level 3:class java.lang.ArithmeticException
In Level 3 finally
In Level 2 finally
In Level 1 finally
4、动手动脑
源码
public static void main(String[] args)
{
try{
System.out.println("in main");
throw new Exception("Exception is thrown in main");
}
catch(Exception e)
System.out.println(e.getMessage());
System.exit(0);
}
finally
{
System.out.println("in finally");
}
}
Exception is thrown in main
class MyException extends Exception
{
public MyException(String messege)
{
super(messege);
}
}
{
public int score(int a) throws MyException //当a<0或a>100时,抛出一个自定义异常
{
if(a<0 || a>100)
{
throw new MyException("成绩输入有误");//抛出异常
}
return a;//返回a
}
}
public static void main(String[] args) throws MyException
{
try
{
Scanner scan=new Scanner(System.in);
System.out.println("请输入分数");
int n = 0,i;
n=scan.nextInt();
numbertest k=new numbertest();
try
{
int t=k.score(n);
i=n/10;
switch(i)
{
case 10:
case 9:
System.out.println("优");break;
case 8:
System.out.println("良");break;
case 7:
System.out.println("中");break;
case 6:
System.out.println("及格");break;
default:
System.out.println("不及格");break;
}
}
catch(MyException e)
{
System.out.println(e);//输出
}
}
catch(Exception e)//由于变量定义为int型,所以输入字符时,则输出该异常信息
{
System.out.println("输入格式不合法");//输出
}
}
}
最新文章
- hdu 3579 Hello Kiki (中国剩余定理)
- CI框架程序--本地调试之后部署新浪SAE
- MQ队列
- POJ 1879 Tempus et mobilius Time and motion 队列和栈
- 在公网上布署Web Api的时候,不能调用,返回404
- CSS常用操作-对齐
- Windows下将ImageMagick移植到Android平台
- 深入探讨List<;>;中的一个姿势。
- HDU 5723 Abandoned country 最小生成树+搜索
- 我的工具:Db SQL Monitor
- 进程间通信(队列、管道)、消费者模型和进程池(apply,apply_async,map)
- Visual studio 配置
- Redis 学习之路 (011) - redis 多数据库
- bzoj1002/luogu2144 轮状病毒 (dp)
- git .gitignore未生效
- Python开课复习9-28
- Chrome插件LiveStyle结合Sublime Text3编辑器实现高效可视化开发
- Centos7以上的版本 mysql 无法启动,无法停止问题
- PAT 1051 Pop Sequence[栈][难]
- spring boot加mybatis使用Map返回时,当值为空时属性也会没有(转)
热门文章
- 【spring boot】SpringBoot初学(9.1)&ndash; 简单配置corsFilter对跨域请求支持
- C语言简单编译预处理-笔记
- 数据结构与算法 C++ 视频教程(4 套)百度网盘
- java-局部变量,成员变量区别
- 我用 Python 集齐了五福
- while语句的练习一
- 在Windows启动pyspark shell:Failed to find Spark jars directory. You need to build Spark before running this program
- laravel打印查询sql
- dubbo之心跳机制
- laravel封装返回json信息工具类