异常(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异常类的更多相关文章

  1. java——异常类、异常捕获、finally、异常抛出、自定义异常

    编译错误:由于编写程序不符合程序的语法规定而导致的语法问题. 运行错误:能够顺利的编译通过,但是在程序运行过程中产生的错误. java异常类都是由Throwable类派生而来的,派生出来的两个分支分别 ...

  2. Java异常类(Throwable)

    一.异常类体系 二.异常类由来与定义 [异常类的由来]:Java把程序在运行时出现的各种不正常情况也看成了对象, 提取属性和行为进行描述,比如异常名称,异常信息,异常发生位置,从而形成了各种异常类 [ ...

  3. java异常类的妙用

    异常类的妙用   以往在使用异常时,只是知道通过异常类的构造方法设置一些出错信息,此外最多就是把引起该异常的原因通过Throwable类的子类一同设置进去.今天在分析springSecurity3.0 ...

  4. Java异常类及处理

    异常概述:运行时发生的不正常情况 在java中用类的形式对不正常的情况进行了描述和封装对象. 描述不正常的类,称之为异常类. 异常就是java通过面向对象的思想将问题封装成了对象,用异常类对其进行描述 ...

  5. 面试准备(三) Java 异常类层次结构

    在Java中,异常分为受检查的异常,与运行时异常. 两者都在异常类层次结构中.这类容易出选择题 考试你是否掌握了异常类并清楚哪些异常类必须捕获 下面的图展示了Java异常类的继承关系. 图1 粉红色的 ...

  6. Java 异常类层次结构

    在Java中,异常分为受检查的异常,与运行时异常. 两者都在异常类层次结构中. 下面的图展示了Java异常类的继承关系. 图1 粉红色的是受检查的异常(checked exceptions),其必须被 ...

  7. 每天一点点java---继承exception类来实现自己的异常类

    package prac_1; /** * <p>Title: 捕获异常和实现自己的异常类</p> * <p>Description: 通过继承Exception类 ...

  8. java语言课堂动手动脑

    1 运行 TestInherits.java 示例,观察输出,注意总结父类与子类之间构造方法的调用关系修改Parent构造方法的代码,显式调用GrandParent的另一个构造函数,注意这句调用代码是 ...

  9. java课堂动手动脑及课后实验总结

      动手动脑一:枚举   输出结果: false false true SMALL MEDIUM LARGE 分析和总结用法 枚举类型的使用是借助ENUM这样一个类,这个类是JAVA枚举类型的公共基本 ...

随机推荐

  1. [洛谷日报第39期]比STL还STL?——pbds

    [洛谷日报第39期]比STL还STL?——pbds   洛谷科技 发布时间:18-08-3116:37 __gnu_pbds食用教程 引入 某P党:“你们C++的STL库真强(e)大(xin),好多数 ...

  2. [经验栈]SQL语句逻辑运算符"AND"、"&&"兼容性

    最近打算把博客转移到typecho平台,选了一个风格个人比较喜欢的主题,即Akina for Typecho 主题模板,在这里先感谢题主的开源分享,但是在使用过程中一开始就出现"500 Da ...

  3. 规则引擎 - drools 使用讲解(简单版) - Java

    drools规则引擎 项目链接 现状: 运维同学(各种同学)通过后台管理界面直接配置相关规则,这里是通过输入框.下拉框等完成输入的,非常简单: 规则配置完毕后,前端请求后端,此时服务端根据参数(即规则 ...

  4. Hadoop点滴-初识MapReduce(1)

    分析气候数据,计算出每年全球最高气温(P25页) Map阶段:输入碎片数据,输出一系列“单键单值”键值对 内部处理,将一系列“单键单值”键值对转化成一系列“单键多值”键值对 Reduce阶段,输入“单 ...

  5. 读《深入理解Elasticsearch》点滴-聚合-top_hits

    以下是官网手册(部分)(v5.1) 直接直接看官网手册 https://www.elastic.co/guide/en/elasticsearch/reference/5.1/search-aggre ...

  6. Spring MVC-从零开始-view-forward、redirect

    1.forward或redirect后,不再走viewResolver过程,直接重新从控制器开始 2.代码 package com.jt; import org.springframework.ste ...

  7. dependencies 与 dependencyManagement 区别

    dependencies 即使在子项目中不写该依赖项,那么子项目仍然会从父项目中继承该依赖项(全部继承)dependencyManagement 里只是声明依赖,并不实现引入,因此子项目需要显示的声明 ...

  8. JPA 已作废的SQLQuery.class、setResultTransformer方法替换

    1.hibernate 5.2 之后,SQLQuery.class.setResultTransformer方法已作废,其用法如下: Query query = entityManager.creat ...

  9. 【爬虫小程序:爬取斗鱼所有房间信息】Xpath(多进程版)

    # 本程序亲测有效,用于理解爬虫相关的基础知识,不足之处希望大家批评指正 import requests from lxml import etree from multiprocessing imp ...

  10. Python爬虫(二):Requests库

    所谓爬虫就是模拟客户端发送网络请求,获取网络响应,并按照一定的规则解析获取的数据并保存的程序.要说 Python 的爬虫必然绕不过 Requests 库. 1 简介 对于 Requests 库,官方文 ...