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("输入格式不合法");//输出
}
}
}

java异常处理课后作的更多相关文章
- java程序中的经常出现的的异常处理课后总结
一.JDK中常见的异常情况 1.常见异常总结图 2.java中异常分类 Throwable类有两个直接子类: (1)Exception:出现的问题是可以被捕获的 (2)Error:系统错误,通常由JV ...
- Java 异常处理笔记
Java程序运行过程中所发生的异常事件可分为两类: §错误(Error):JVM系统内部错误.资源耗尽等严重情况 §违例(Exception): 其它因编程错误或偶然的外在因素导致的一般性问题,例如: ...
- 谈谈你对Java异常处理机制的理解
先谈谈我的理解:异常处理机制可以说是让我们编写的程序运行起来更加的健壮,无论是在程序调试.运行期间发生的异常情况的捕获,都提供的有效的补救动作,任何业务逻辑都会存在异常情况,这时只需要记录这些异常情况 ...
- 理解java异常处理机制
1. 引子 try…catch…finally恐怕是大家再熟悉不过的语句了,而且感觉用起来也是很简单,逻辑上似乎也是很容易理解.不过,我亲自体验的“教训”告诉我,这个东西可不是想象中的那么简单.听话. ...
- 2017.4.7 java异常处理总结
目录 1.java异常处理的几种错误做法 2.异常处理示例 3.常用异常 4.异常类的继承关系 5.异常处理机制 6.Throw和Throws的区别 7.e.toString(), e.getCaus ...
- Java异常处理总结Exception\Error
Java异常处理总结Exception\Error 2012-12-28 08:17:17| 分类: JAVA | 标签:java |举报|字号 订阅 Java异常处理总结 ...
- 札记:Java异常处理
异常概述 程序在运行中总会面临一些"意外"情况,良好的代码需要对它们进行预防和处理.大致来说,这些意外情况分三类: 交互输入 用户以非预期的方式使用程序,比如非法输入,不正当的操作 ...
- java异常处理(父子异常的处理)
我当初学java异常处理的时候,对于父子异常的处理,我记得几句话“子类方法只能抛出父类方法所抛出的异常或者是其子异常,子类构造器必须要抛出父类构造器的异常或者其父异常”.那个时候还不知道子类方法为什么 ...
- Java 异常处理
异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的. 比如说,你的代码少了一个分号,那么运行出来结果是提示是错误java.lang.Error:如果你用System.out ...
随机推荐
- win10系统vscode c/c++配置环境
使用vscode可以完成轻量级的编译器环境搭建,使用vscode 搭建完整的c++开发环境见下网站: https://www.bilibili.com/video/av18436497/?spm_id ...
- .Net 程序代码混淆加密工具 ILProtector
我的项目中某一部分信息比较敏感,但是.Net程序反编译之后连注释都看得到.需要把exe保护起来,如:代码混淆之后再加壳. Bing到一款.Net混淆工具 ILProtector 作为资深工具党, ...
- 使用pem连接服务器
后台同学甩给你一个pem文件,username@IP后如何链接服务器 准备:ssh客户端 例子xshell 文件->新建->主机(连接界面主机输入框输入IP)->点击用户身份-> ...
- c++编程之内存模型
我们在编程的时候,无可避免要申明变量,在这个变量可以是在()中,可以在{}中,也可以直接在外面,也可以用new的方式.那么当我们在申明变量的时候,实质上我们所做的工作是:关联了一个内存模型! 上代码: ...
- jQuery笔记(四)jQuery中的动画
jQuery最吸引人的地方莫过于能做出绚丽的动画了,也是能极大提高用户体验的地方,这次我们就来一探jQuery中的动画! 一. show()方法和hide()方法 show()方法与hide()方法是 ...
- PAT (Advanced Level) Practice 1006 Sign In and Sign Out (25 分) (排序)
At the beginning of every day, the first person who signs in the computer room will unlock the door, ...
- vue中的this.$nextTick()使用
首先我们来翻译一下nextTick是什么意思:下一个刻度 再来看看vue官网怎么说的: Vue.nextTick( [callback, context] )在下次 DOM 更新循环结束之后执行延迟回 ...
- 微信小程序报错TypeError: this.setData is not a function
今天在练习小程序的时候,遇到小程序报错 对于处于小白阶段的我,遇到这种报错,真还不知道是错从何来,只有一脸蒙逼,后来通过查询,终于知道了问题所在,下面对这一问题做一记录 小程序默认中是这么写的 onL ...
- 零基础入门 Kubernetes,你需要知道这些
Kubernetes是什么? 大概很多人对此都有疑问,不过在容器领域,Kubernetes却无人不晓. 阿里.字节跳动.腾讯.百度等中国互联网行业巨擘们,近年来都在深耕容器领域,而Kubernetes ...
- Unknown CMake command "check_symbol_exists".
- Using these message generators: gencpp;geneus;genlisp;gennodejs;genpyCMake Error at CMakeLists.txt ...