异常(实际使用直接try-catch)

1.常见系统异常

异常

异常的解释

ClassNotFoundException

未找到要装载的类

ArrayIndexOutOfBoundsException

数组访问越界

FileNotFoundException

文件找不到

IOException

输入、输出错误

NullPointerException

空指针访问

ArithmeticException

算术运算错误,如除数为0

NumberFormatException

数字格式错误

InterruptedException

中断异常。

2.自定义异常

(1)定义异常类—继承Exception类

(2)在方法内抛出异常

throw new 异常类();

(3)声明方法存在异常

在方法头的尾部加上:throws 异常类列表

3.实战

1.假设学校开设舞蹈课程,输入男生、女生的数量,如果男生或女生的数量为0,则舞蹈课程无法开设,如果不为0,则根据男生/女生人数的多少计算男生/女生的partner数量。请用异常类处理数量为0的情况,重新编写下面的代码。

要求运行结果如下:

主函数

public class DancerLesson {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
try{
System.out.println("Enter number of male dancers:");
int men=input.nextInt();
System.out.println("Enter number of female dancers:");
int women=input.nextInt();
if(men==0||women==0)
throw new myExcpetion(men,women);
else
{
if(men>=women)
System.out.println("Each woman must dance with"+men/(double)women+"men.");
else
System.out.println("Each man must dance with"+women/(double)men+"women.");
System.out.println("Begin the lesson");
}
}catch(myExcpetion e) {
System.out.println(e);
}
}
}

异常类

public class myExcpetion extends Exception {
public String tostring() {
return "人数错误";
} public myExcpetion(int men,int women) {
System.out.print("Lesson is canceled.");
if(men==0&&women==0)
System.out.println("No student");
else if(men>0&&women==0)
System.out.println("No men");
else
System.out.println("No women");

结果

2.要求声明定义两个Exception的异常子类:NoLowerLetter类和NoDigit类

再声明一个People类,该类中的void printLetter(char c)方法抛出NoLowerLetter异常类对象,void

printDigit(char c)方法抛出NoDigit异常类对象。请补充下列代码中【代码1】到【代码6】

//ExceptionExample.java
public class NoLowerLetter // 类声明,声明一个Exception的子类NoLowerLetter
{
public void print() {
System.out.printf("%c",'#');
}
}
public class NoDigit extends Exception // 类声明,声明一个Exception的子类NoDigit
{
public void print() {
System.out.printf("%c",'*');
}
}
class People {
void printLetter(char c) throws NoLowerLetter {
if(c<'a'||c>'z') {
NoLowerLetter noLowerLetter= new NoLowerLetter(); // 创建NoLowerLetter类型对象
throw noLowerLetter; // 抛出noLowerLetter
}
else {
System.out.print(c);
}
}
void printDigit(char c) throws NoDigit {
if(c<'1'||c>'9') {
NoDigit noDigit= new NoDigit(); // 创建NoDigit()类型对象
throw noDigit; // 抛出noDigit
}
else {
System.out.print(c);
}
}
}
public class ExceptionExample {
public static void main (String args[ ]){
People people=new People( );
for(int i=0;i<128;i++) {
try
{
people.printLetter((char)i);
}
catch(NoLowerLetter e)
{
e.print();
}
}
for(int i=0;i<128;i++) {
try
{
people.printDigit((char)i);
}
catch(NoDigit e)
{
e.print( );
}
}
}
}

3.选择题

1)设有如下代码:

 try {
tryThis();
return;
} catch (IOException x1) {
System.out.println("exception 1");
return;
} catch (Exception x2) {
System.out.println("exception 2");
return;
} finally {
System.out.println("finally");
}

如果tryThis() 抛出 NumberFormatException,则输出结果是? (C)

A. 无输出

B. "exception 1", 后跟 "finally"

C. "exception 2", 后跟 "finally"

D. "exception 1"

E. "exception 2"

2)设有如下方法:

public void test() {
try { oneMethod();
System.out.println("condition 1");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("condition 2");
} catch(Exception e) {
System.out.println("condition 3");
} finally {
System.out.println("finally");
}
}

如果oneMethod正常运行,则输出结果中有哪些?(AD)

A. condition 1

B. condition 2

C. condition 3

D. finally

3) 设有如下代码:

public void fun () {
int i;
try
{
i=System.in.read ();
System.out.println("Location 1");
} catch (IOException e) {
System.out.println("Location 2");
} finally {
System.out.println("Location 3");
}
System.out.println("Location 4");
}

如果有一个IOException发生, 则输出有哪些?    (BCD)

A. Location 1

B. Location 2

C. Location 3

D. Location 4

4)  设有如下代码:

1 String s = null
2 if ( s != null & s.length() > 0)
3 System.out.println("s != null & s.length() > 0"); 4 if ( s != null && s.length() > 0)
5 System.out.println("s != null & s.length() > 0"); 6 if ( s != null || s.length() > 0)
7 System.out.println("s != null & s.length() > 0"); 8 if ( s != null | s.length() > 0)
9 System.out.println("s != null | s.length() > 0");

以下行中哪些会产生空指针异常。                (D)

A. 2,4

B. 6,8

C. 2,4,6,8

D. 2,6,8

5) 类Test1、Test2定义如下:

1.public class  Test1 {
2. public float aMethod(float a,float b) throws IOException {
3. }
4. }
5. public class Test2 extends Test1{
6.
7.

将以下哪种方法插入行6是不合法的。              (A)

A、float  aMethod(float  a,float  b){ }

B、public  int  aMethod(int a,int b)throws  Exception{ }

C、public  float  aMethod(float  p,float q){ }

D、public  int  aMethod(int a,int  b)throws IOException{ }

java 实验4 异常的更多相关文章

  1. Java实验链接

    第1次实验 课堂实验内容:Java入门+Eclipse+PTA+Git+博客 实验任务书:第01次试验(安装JDK.编辑器.编写出第一个Java程序).pdf Eclipse简明教程(by郑如滨).p ...

  2. 20175212童皓桢 Java实验二-面向对象程序设计实验报告

    20175212童皓桢 Java实验二-面向对象程序设计实验报告 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设 ...

  3. 20165310 Java实验五《网络编程与安全》

    20165310 Java实验五<网络编程与安全> 任务一 题目:①编写MyBC.java实现中缀表达式转后缀表达式的功能:②编写MyDC.java实现从上面功能中获取的表达式中实现后缀表 ...

  4. Java实验报告&&课程报告

    Java实验报告 班级 计算机科学与技术二班 学号 20188450 姓名 李代传 完成时间 2019/9/19 评分等级 实验二 Java简单类与对象 实验目的 掌握类的定义,熟悉属性.构造函数.方 ...

  5. 第二次Java实验报告

    Java实验报告 班级 计科二班 学号 20188437 姓名 何磊 完成时间 2019/9/12 评分等级 实验二 Java简单类与对象 实验目的 掌握类的定义,熟悉属性.构造函数.方法的作用,掌握 ...

  6. 第七次java实验报告

    Java实验报告 班级 计科二班 学号20188437 姓名 何磊 完成时间 2019/10/25 评分等级 实验四 类的继承 实验内容 )总票数1000张:(2)10个窗口同时开始卖票:(3)卖票过 ...

  7. 第六次java实验报告

    Java实验报告 班级 计科二班 学号20188437 姓名 何磊 完成时间 2019/10/17 评分等级 实验四 类的继承 实验目的 理解异常的基本概念: 掌握异常处理方法及熟悉常见异常的捕获方法 ...

  8. 第九周Java实验作业

    实验九 异常.断言与日志 实验时间 2018-10-25 1.实验目的与要求 (1) 掌握java异常处理技术: Java的异常处理机制可以控制程序从错误产生的位置转移到能够进行错误处理的位置. Ja ...

  9. Core Java 总结(异常类问题)

    所有代码均在本地编译运行测试,环境为 Windows7 32位机器 + eclipse Mars.2 Release (4.5.2) 2016-10-17 整理 下面的代码输出结果是多少?为什么?并由 ...

随机推荐

  1. 记一次结巴分词.net core 2.0版 nuget发布过程

    最近用到分词考虑很久,选用了结巴分词,原因见博客Lucene.net(4.8.0) 学习问题记录五: JIEba分词和Lucene的结合,以及对分词器的思考 既然选好了,难就开始行动吧 . 查了.ne ...

  2. layer插件open方法回掉值问题

    最近做项目需用到一个弹出层加载iframe页面的东西,首先想到layer插件,此插件用到过多次,兼容性很好,功能也强大,废话不多说上代码. 其实功能很简单,就是在目标页面选择一个值,回掉回来,说明一下 ...

  3. Centos7手动编译安装Python3

    Python3的安装方式非常的简单,首先去到Python下载目录下载想要的Python包,这里我选择了3.6.5版本 $ wget https://www.python.org/ftp/python/ ...

  4. synchronized的简单理解

    synchronized能够保证在同一时刻只有一个线程执行该段代码. 使用synchronized能够防止多个线程同时并发访问程序的某些资源. synchronized既可以修饰变量,也可以修饰方法, ...

  5. Intellij IDEA 启动项目ClassNotFoundException

    博客原文地址:https://blog.csdn.net/wo541075754/article/details/45640267 使用Intellij IDEA  的过程中,新创建的项目启动时报错: ...

  6. 面试概率极大的Oracle存储过程

    1.什么是存储过程.存储过程是数据库服务器端的一段程序,它有两种类型.一种类似于SELECT查询,用于检索数据,检索到的数据能够以数据集的形式返回给客户.另一种类似于INSERT或DELETE查询,它 ...

  7. jquery中的属性和样式设置

    添加属性 $target.attr({"title":"one piece","name":"solgan"}); 为目 ...

  8. 关于Yii2.0的url路径优化问题(配置虚拟路径)

    backend/config/main.php 'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => fa ...

  9. XSS学习笔记

    本片文章是读<<XSS跨站脚本gj剖析与防御>>一书的总结 常见的XSS攻击主要用于1.网络钓鱼,盗用用户账号2.窃取cookies 非httponly情况下,读取docume ...

  10. linux下PHP7安装memcache

    1.memcache服务器的安装 .分别把memcached和libevent下载回来,放到 /tmp 目录下: # cd /tmp # wget http://www.danga.com/memca ...