【2023级】智能医学工程专业-计算机程序设计-第05章-程序调试
作者: 阮晓龙 发布时间: 2024/9/13 9:00:56
基本信息:
章节名称:智能医学工程专业-计算机程序设计-第05章-程序调试
授课教师:信息技术学院互联网技术教学团队
完成时间:2024年09月
适用年级:2023级
适用专业:智能医学工程
文档内容:
讲稿课件(教学版):智能医学工程-计算机程序设计-课件-第05章-程序调试-教学版.pdf
讲稿课件(笔记版):智能医学工程-计算机程序设计-课件-第05章-程序调试-笔记版.pdf
讲稿课件(在线版):
学习资源:
代码:eg5.2
public class Main {
public static void main(String[] args) {
try {
int result = 10 / 0; // 可能抛出ArithmeticException
} catch (ArithmeticException e) {
System.out.println("除数不能为零!");
}
}
}代码:eg5.3
public class Main {
public static void checkAge(int age) throws Exception {
if (age < 18) {
throw new Exception("未成年禁止入内");
}
}
public static void main(String[] args) {
try {
checkAge(15); // 可能抛出Exception
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}代码:eg5.4
public class Main {
public static void main(String[] args) {
try {
int[] numbers = {1, 2, 3};
System.out.println(numbers[3]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("数组索引越界!");
} finally {
System.out.println("程序执行结束。");
}
}
}代码:eg5.5
public class Main {
public static void main(String[] args) {
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("错误信息: " + e.getMessage());
e.printStackTrace();
}
}
}代码:eg5.6
public class Main {
public static void main(String[] args) {
try {
int[] numbers = {1, 2, 3};
System.out.println(numbers[3]);
int result = 10 / 0;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("数组索引越界!");
} catch (ArithmeticException e) {
System.out.println("除数不能为零!");
}
}
}代码:eg5.7
class CustomException extends Exception {
public CustomException(String message) {
super(message);
}
}
public class Main {
public static void checkNumber(int number) throws CustomException {
if (number < 0) {
throw new CustomException("数字不能为负数");
}
}
public static void main(String[] args) {
try {
checkNumber(-5);
} catch (CustomException e) {
System.out.println("捕获自定义异常: " + e.getMessage());
}
}
}软件资源:

