课堂动手动脑验证以及自定义异常类实现对异常处理——java异常类
异常(exception):发生在程序执行期间,表明出现了一个非法运行的情况。许多JDK中的方法在检测到非法情况时,都会抛出一个异常对象。例如:数组越界和被0除。
代码验证:
package test; import javax.swing.*; class AboutException {
public static void main(String[] a)
{
int i=1, j=0, k;
try
{ 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的情况时,一定要注意程序执行的先后顺序。
多try catch的java异常处理代码一
package test; 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");
}
}
}
程序运行结果:
多try catch的java异常处理代码二
package test; 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");
}
}
}
程序运行结果:
当有多个try catch finally嵌套 时,要特别注意finally的执行时机。特别注意:当有多层嵌套的finally时,异常在不同的层次抛出,在不同的位置抛出,可能会导致不同的finally语句的执行顺序。再者,我们一般认为finally语句中的句子一定会被执行,这里的一定是相对而言的,并不是绝对的。例如以下程序代码的运行:
package test; public class SystemExitAndFinally { public static void main(String[] args)
{ try{ System.out.println("in main"); throw new Exception("Exception is thrown in main"); //System.exit(0); } catch(Exception e) { System.out.println(e.getMessage()); System.exit(0); } finally { System.out.println("in finally"); } } }
运行结果:
在这段java代码中finally语句块并没有执行。
通过过异常的学习,自己尝试了自定义了一个异常类来处理异常,源码如下:
package classtest; import java.util.Scanner; class Myexception extends Exception {
public Myexception(String message) {
super(message);
}
} public class Mytest { public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
try{
function();
}
catch (Exception e) {
System.out.println(e.getMessage());
System.out.println("main()函数运行时出现异常");
}
finally {
System.out.println("main()函数运行完毕");
}
} public static int judge(String str) {
int c = 0;
String regex = "[0-9]+";
String regex1="[-][0-9]+";
boolean d = str.matches(regex);
boolean e = str.matches(regex1);
if (d == false) {
c = 1;
if (e == true) {
c = 0;
}
}
return c;
} public static void function() {
try {
System.out.println("请输入一个正整数:");
Scanner input = new Scanner(System.in);
String a = input.next();
int temp = judge(a);
if (temp == 1) {
throw new Myexception("不能输入非法字符");
} else {
int num = Integer.parseInt(a);
if (num < 0)
throw new Myexception("输入不能为负数");
else if (num == 0) {
throw new Myexception("正整数不包括0");
}
} } catch (Myexception f) {
System.out.println(f.getMessage());
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println("function()函数运行时出现异常");
}
finally {
System.out.println("function()函数已运行完毕,请指示下一步动作");
} }
}
课堂动手动脑验证以及自定义异常类实现对异常处理——java异常类的更多相关文章
- java——异常类、异常捕获、finally、异常抛出、自定义异常
编译错误:由于编写程序不符合程序的语法规定而导致的语法问题. 运行错误:能够顺利的编译通过,但是在程序运行过程中产生的错误. java异常类都是由Throwable类派生而来的,派生出来的两个分支分别 ...
- Java异常类(Throwable)
一.异常类体系 二.异常类由来与定义 [异常类的由来]:Java把程序在运行时出现的各种不正常情况也看成了对象, 提取属性和行为进行描述,比如异常名称,异常信息,异常发生位置,从而形成了各种异常类 [ ...
- java异常类的妙用
异常类的妙用 以往在使用异常时,只是知道通过异常类的构造方法设置一些出错信息,此外最多就是把引起该异常的原因通过Throwable类的子类一同设置进去.今天在分析springSecurity3.0 ...
- Java异常类及处理
异常概述:运行时发生的不正常情况 在java中用类的形式对不正常的情况进行了描述和封装对象. 描述不正常的类,称之为异常类. 异常就是java通过面向对象的思想将问题封装成了对象,用异常类对其进行描述 ...
- 面试准备(三) Java 异常类层次结构
在Java中,异常分为受检查的异常,与运行时异常. 两者都在异常类层次结构中.这类容易出选择题 考试你是否掌握了异常类并清楚哪些异常类必须捕获 下面的图展示了Java异常类的继承关系. 图1 粉红色的 ...
- Java 异常类层次结构
在Java中,异常分为受检查的异常,与运行时异常. 两者都在异常类层次结构中. 下面的图展示了Java异常类的继承关系. 图1 粉红色的是受检查的异常(checked exceptions),其必须被 ...
- 每天一点点java---继承exception类来实现自己的异常类
package prac_1; /** * <p>Title: 捕获异常和实现自己的异常类</p> * <p>Description: 通过继承Exception类 ...
- java语言课堂动手动脑
1 运行 TestInherits.java 示例,观察输出,注意总结父类与子类之间构造方法的调用关系修改Parent构造方法的代码,显式调用GrandParent的另一个构造函数,注意这句调用代码是 ...
- java课堂动手动脑及课后实验总结
动手动脑一:枚举 输出结果: false false true SMALL MEDIUM LARGE 分析和总结用法 枚举类型的使用是借助ENUM这样一个类,这个类是JAVA枚举类型的公共基本 ...
随机推荐
- HTML5-常用正则表达式
有关H5正则表达式的一些常用式子,希望热爱编程的同学们多多指教,还有也希望可以关注收藏本站哦!❤^_^❤ 一.校验数字的表达式 1. 数字:^[0-9]*$ 2. n位的数字:^\d{n}$ 3. 至 ...
- Spring boot 梳理 -@SpringBootApplication、@EnableAutoConfiguration与(@EnableWebMVC、WebMvcConfigurationSupport,WebMvcConfigurer和WebMvcConfigurationAdapter)
@EnableWebMvc=继承DelegatingWebMvcConfiguration=继承WebMvcConfigurationSupport 直接看源码,@EnableWebMvc实际上引入一 ...
- Ceph 的 'MAX AVAIL' 和 数据平衡 - Storage 6
1. 客户环境 节点数量:4个存储节点 OSD数量:每个节点10块8GB磁盘,总共 40 块OSD Ceph 版本: Storage 6 使用类型: CephFS 文件 CephFS数据池: EC ...
- Flask框架踩坑之ajax跨域请求
业务场景: 前后端分离需要对接数据接口. 接口测试是在postman做的,今天才开始和前端对接,由于这是我第一次做后端接口开发(第一次嘛,问题比较多)所以在此记录分享我的踩坑之旅,以便能更好的理解,应 ...
- (三)分布式数据库tidb-隔离级别详解
tidb隔离级别详解: 1.TiDB 支持的隔离级别是 Snapshot Isolation(SI),它和 Repeatable Read(RR) 隔离级别基本等价,详细情况如下: ● TiDB 的 ...
- VR应用评测 - Google Spotlight Story: Sonaria
Google Spotlight Story: Sonaria 一个5min左右的VR小电影,坐姿观看,但是用户其实可以移动+旋转视角.画面很抽象,所有的物体都由基本的单色几何形状组成,主角是两个一公 ...
- phpexcel 导出方法
Vendor("PHPExcel.PHPExcel"); Vendor("PHPExcel.PHPExcel.IOFactory"); Vendor(" ...
- 怎样实现给DEDE5.7的栏目增加栏目图片
前两天用DEDE做二次开发的时候,遇到一个问题,领导让给每个栏目增加一个栏目图片的功能,网上找了些东西,结合自己实际做的时候的方法,下面详细描述下具体的实现方式(只测试了V5.7版本,对低版本是否适用 ...
- linux分析工具之vmstat详解
一.概述 vmstat命令是最常见的Linux/Unix监控工具,可以展现给定时间间隔的服务器的状态值,包括服务器的CPU使用率,内存使用,虚拟内存交换情况,IO读写情况.首先我们查看下帮助.如下图所 ...
- 【SQL】sql查询同一字段相同属性列的值合计
select type,sum(value) as valueSum from t group by type