java程序设计实验报告七_异常

Java中的异常

异常的抛出

throw

通过[^throw new 异常类名();]可以抛出一个异常(默认初始化)。

public class ThrowTest {

public static void main(String[] args) {

Integer a = 1;

Integer b = null;

if (a == null || b == null) {

throw new NullPointerException();

} else {

System.out.println(a + b);

}

}

}

Exception in thread "main" java.lang.NullPointerException

at ThrowTest.main(ThrowTest.java:8)

throws

关键字throws用于修饰方法,表示该方法可能抛出某异常,并交由上一层处理。对于受检异常,若方法内不能处理,则必须向上抛出,交给上一层处理。throws也可以修饰main方法。

9397454ef65b6ec3a84f57672d9524c0.png

3caa530c1ed65c2cddcd01c148f30e62.png

检测点

45edf5d10daa4808561e65b2f112671d.png

try-catch块

try-catch块用于执行可能抛出异常的代码,catch块将捕获抛出的异常并进行相应的处理。在try,catch之后还可以加上一个finally块,当try-catch块执行结束后,该代码块将被执行。

e5a741660e02dcca712a3181b830c8e3.png

3aa71626fd0a4e55feb587a7ac1d5bb7.png

检测点

8f8de77b6848ee658eb6ca74782fffab.png

捕获多个异常

try块后面可以跟多个catch块,用于捕获不同类型的异常。抛出异常后,catch块会按顺序检测异常是否为它所捕获的类型或其子类。当一个catch块被执行后,后面的catch块将被跳过。故子类异常的catch块应放在父类异常类前面,否则将形成死代码。

f5493ce76c0d994fabc27cfc251098f4.png

8905caf45cf9457e9b605fc11e946d79.png

检测点

cd0f5e05a52c17c74c3b265756e0f97e.png

自定义异常

实际编程中异常种类是非常繁杂的,为了更清楚地表达异常的含义,我们可以自定义异常。自定义异常很简单,我们只需定义一个继承自任意已知异常类的类就行了,并按照需要定义参数与父类一致的构造方法,并在构造方法里面直接调用父类构造方法就可以了。如图

df082e211ce85eb48ecb4c01ab79334a.png

由于异常类的公共父类Exception实现了Serializable(可序列化)接口,故需要定义一个

private final static long serialVersionUID = 12347901348033L;

来进行表识。

处理这个异常的方法与api里面异常是一样的

测试类

d097aeeb0d3dbc1a6ccb193ae233bb19.png

1d02b2203bae8c8fb12b3d7595bfd7b4.png

检测点

3a838f3d64440c207c61ea08643c7478.png

printStackTrace

这个方法可以用来输出抛出异常的栈轨迹,据此可以知道异常是在哪个文件的哪一行抛出的。可以方便开发人员查错。

如图

public class ExceptionStackTrace {

private static void method1() {

method2();

}

private static void method2() {

throw new NullPointerException();

}

public static void main(String[] args) {

try {

method1();

} catch (Exception e) {

e.printStackTrace();

}

}

}

输出

java.lang.NullPointerException

at ExceptionStackTrace.method2(ExceptionStackTrace.java:7)

at ExceptionStackTrace.method1(ExceptionStackTrace.java:3)

at ExceptionStackTrace.main(ExceptionStackTrace.java:11)

检测点

030b37e84afa02ff95adc2217bd1e13b.png

最终通过截图

bc0363462981d7f86d08e950c6e4202c.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实验 Java多线程 一、实验目的: 熟悉利用Thread类建立多线程方法。 熟悉利用Thread接口建立多线程方法。 二、实验内容: 1. 阅读下列程序,分析并上机检验其功能。 class DelayThread exends Thread{ private static int count=0; private int no; private int delay; public DelayThread(){ count++; no=count; } public void run(){ try{ for (int i=0;i<10;i++){ delay=(int)(Math.random()*5000); sleep(delay); System.out.println(“Thread ”+no+” with a delay ”+delay); } }catch(InterruptedException e){}}} public class MyThread{ public static void main(String args[]){ DelayThread thread1=new DelayThread(); DelayThread thread2=new DelayThread(); thread1.start(); thread2.start(); try{ Thread.sleep(1000);}catch(InterruptedException e){ System.out.println(“Thread wrong”);}}} 2.讲上列程序利用Runnable接口改写,并上机检验。 3.利用多线程编写一个模拟时钟(AWT程序、Runnable接口),有时/分/秒针 编写一个应用程序,创建三个线程分别显示各自的时间。 三、实验要求: 1. 通过实验掌握Thread 、Runnable使用方法; 2. 程序必须能够实现多线程; 3. 程序必须能够完成题目要求; 4. 写出实验报告。 四、实验步骤: 首先分析程序功能,再通过上机运行验证自己的分析,从而掌握通过Thread类建立多线程的方法。 通过将扩展Thread类建立多线程的方法改为利用Runnable接口的方法,掌握通过Runnable接口建立多线程的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值