异常的体系:

----------| Throwable  所以异常或者错误类的超类

--------------|Error  错误   错误一般是用于jvm或者是硬件引发的问题,所以我们一般不会通过代码去处理错误的。

--------------|Exception 异常   是需要通过代码去处理的。

如何区分错误与异常呢:
如果程序出现了不正常的信息,如果不正常的信息的类名是以Error结尾的,那么肯定是一个错误。

如果是以Exception结尾的,那么肯定就是一个异常。

异常的处理:

方式一:捕获处理(往下面还有方式二

 捕获处理的格式:

try{
可能发生异常的代码;

}catch(捕获的异常类型 变量名){
处理异常的代码....
}

捕获处理要注意的细节:
1.code1中体现→如果try块中代码出了异常经过了处理之后,那么try-catch块外面的代码可以正常执行。
2.code2中体现→如果try块中出了异常的代码,那么在try块中出现异常代码后面的代码是不会执行了。
3.code3中体现→一个try块后面是可以跟有多个catch块的,也就是一个try块可以捕获多种异常的类型。
4.code4中体现→一个try块可以捕获多种异常的类型,但是捕获的异常类型必须从小到大进行捕获,否则编译报错。

code1

package day10;

public class TestException2 {

	public static void main(String[] args){
test(1,0);
}
public static void test(int a , int b){
try{
int c = a/b;
}catch (ArithmeticException e){
System.out.println("异常处理了 并返回当前异常对象的完整类名+病态信息 :"+e.toString());
}
System.out.println("try-catch块外面的代码可以正常执行中");
}
}

运行结果:

异常处理了 回当前异常对象的完整类名+病态信息 :java.lang.ArithmeticException: / by zero

try-catch块外面的代码可以正常执行中

code2

package day10;

public class TestException2 {

	public static void main(String[] args){
test(1,0);
}
public static void test(int a , int b){
try{
int c = a/b;
System.out.println("看看是否被执行到~");
}catch (ArithmeticException e){
System.out.println("异常处理了 并返回当前异常对象的完整类名+病态信息 :"+e.toString());
}
}
}

运行结果:

异常处理了  并返回回当前异常对象的完整类名+病态信息 :java.lang.ArithmeticException: / by zero

”看看是否被执行到~“  这句没有被执行到,因为int c = a/b;这句出现异常了,所以try块内的其余代码都不会执行,直接会被catch 处理掉。

code3

public class TestException2 {

	public static void main(String[] args){
int[] arr = null;
test(10,2,arr); }
public static void test(int a , int b, int[] arr) {
int c = 0 ;
try{ c = a/b;
System.out.println(arr.length);
}catch (ArithmeticException e){
System.out.println("异常处理了 并返回回当前异常对象的完整类名+病态信息 :"+e.toString());
}catch (NullPointerException e){
System.out.println("出现了空指针异常....");
}catch (Exception e){
System.out.println("cc");
}
System.out.println("c="+c);
}
}

运行结果:

出现了空指针异常....

c=5

code3这段代码要注意,try块能检测多种异常,但是只能处理一种异常,就是第一个异常。

code4

public class TestException2 {

	public static void main(String[] args){
int[] arr = null;
test(10,2,arr); }
public static void test(int a , int b, int[] arr) {
int c = 0 ;
try{ c = a/b;
System.out.println(arr.length);
}catch (Exception e){
System.out.println("Exception类来捕获的话,其他类就捕获不了,相当于父类把工作都做完了,还有子类的事吗?");
}catch (NullPointerException e){
System.out.println("出现了空指针异常....");
}catch (ArithmeticException e){
System.out.println("cc");
}
System.out.println("c="+c);
}
}

运行结果:

Exception  把   NullPointerException  和  ArithmeticException    的异常都捕获处理了,所以会提示错误,必须从小到大进行捕获,否则编译报错

方式二:异常的处理方式----抛出处理

抛出处理要注意的细节:
1.code5中体现→ 如果一个方法的内部抛出了一个异常 对象,那么必须要在方法上声明抛出。
2.code6中体现→如果调用了一个声明抛出异常 的方法,那么调用者必须要处理异常。
3.code7中体现→如果一个方法内部抛出了一个异常对象,那么throw语句后面的代码都不会再执行了(一个方法遇到了throw关键字,该方法也会马上停止执行的)。
4.code8中体现→在一种情况下,只能抛出一种类型异常对象。(意思就是if())

throw 与throws两个关键字:
1. throw关键字是用于方法内部的,throws是用于方法声声明上的。
2. throw关键字是用于方法内部抛出一个异常对象的,throws关键字是用于在方法声明上声明抛出异常类型的。
3. throw关键字后面只能有一个异常对象,throws后面一次可以声明抛出多种类型的 异常

概念

※Exception在程序中必须使用try...catch进行处理。

※RuntimeException可以不使用try...catch进行处理,但是如果有异常产生,则异常将由JVM进行处理(默认往上抛)

code5

public class TestException2 {

	public static void main(String[] args){
try{
int[] arr = null;
test(10,2,arr);
}catch(NullPointerException e){
System.out.println("报告! 有异常出现");
e.printStackTrace();
} }
public static void test(int a , int b, int[] arr) throws ArithmeticException,NullPointerException{
int c = 0 ;
if(b==0){
throw new ArithmeticException(); //抛出一个异常对象...
}else if(arr==null){
throw new NullPointerException();
} c = a/b;
System.out.println(arr.length); System.out.println("c="+c);
}
}

运行结果:

报告!  有异常出现
java.lang.NullPointerException
at day10.TestException2.test(TestException2.java:22)
at day10.TestException2.main(TestException2.java:10)

code6

public class TestException2 {

	public static void main(String[] args)
{
//try{
int[] arr = null;
test(4,0,arr); //调用了一个 声明抛出异常类型 的方法
//}catch(Exception e){
System.out.println("报告! 异常出现了");
// e.printStackTrace();
//} }
public static void test(int a , int b, int[] arr) throws Exception,NullPointerException{
int c = 0 ;
if(b==0){
throw new Exception(); //抛出一个异常对象...
}else if(arr==null){
throw new NullPointerException();
} c = a/b;
System.out.println(arr.length); System.out.println("c="+c);
}
}

运行结果:

P.S.

这code6中的代码 ,一定要注意 在test方法抛出的如果是RuntimeException类的话 ,就是不捕获也不会提示报错的,因为异常分为检查性异常和运行时异常 检查性异常需要捕捉,运行时异常不会强制要求捕捉,也就是说非RuntimeException  包括Eception  都是需要捕获

code7

public class TestException2 {

	public static void main(String[] args)
{
try{
int[] arr = null;
test(4,0,arr); //调用了一个 声明抛出异常类型 的方法
}catch(Exception e){
System.out.println("报告! 异常出现了");
e.printStackTrace();
} }
public static void test(int a , int b, int[] arr) throws Exception,NullPointerException{
int c = 0 ;
if(b==0){
throw new Exception(); //抛出一个异常对象...
}else if(arr==null){
throw new NullPointerException();
} c = a/b; System.out.println(arr.length); System.out.println("c="+c);
}
}

运行结果:

报告! 异常出现了

java.lang.Exception
at day10.TestException2.test(TestException2.java:21)
at day10.TestException2.main(TestException2.java:11)

可见C的值没有打印出来,所以一抛出异常 后面的代码都不执行了。

code8

class Demo11
{
public static void main(String[] args)
{
try{
int[] arr = null;
div(10,0,arr); //调用了一个 声明抛出异常类型 的方法
}catch(Exception e){
System.out.println("报告! 出现异常了");
e.printStackTrace();
} } public static void div(int a, int b,int[] arr) throws Exception,NullPointerException {
if(b==0 || arr==null){
throw new Exception(); //抛出一个异常对象...
throw new NullPointerException();
}
int c = a/b;
System.out.println("c="+c);
}
}

运行结果:

因为抛出了异常 就不会处理下面的语句了,方法都结束了 ,所以只能将2个异常分开成两种情况 才能避免编译时错误。

if(b==0){
throw new Exception(); //抛出一个异常对象...
}else if(arr==null){
throw new NullPointerException();
}

交流企鹅:654249738,和自学者交流群:517284938

JAVA_SE基础——54.异常的更多相关文章

  1. JAVA_SE基础——6.标识符&关键字

    学会写helloworld之后,  我们就开始来认识标识符&关键字 一.标识符 标识符是指可被用来为类.变量或方法等命名的字符序列,换言之,标识符就是用户自定义的名称来标识类.变量或方法等.更 ...

  2. JAVA_SE基础——26.[深入解析]局部变量与成员变量的区别

    黑马程序员入学blog ... 如果这章节很难懂的话应该返回去先看  JAVA_SE基础--10.变量的作用域 定义的位置上区别: 1. 成员变量是定义在方法之外,类之内的. 2. 局部变量是定义在方 ...

  3. JAVA_SE基础——9.基本数据类型间的转换

    前面我已经教会大家基本的数据类型进行了介绍,   然后这篇文章,我来介绍下,基本数据类型的转换. Java中有两种类型转换形式,分别是自动类型转换和强制类型转换. Step1.自动类型转换. 自动类型 ...

  4. JAVA_SE基础——8.基本数据类型

    基本数据类型有:整数类型.浮点类型.字符类型.布尔类型 整数类型 整数类型用来存储整数数值,即没有小数部分的数值.与C.C++语言相同,整数在Java语言中有3种表示形式:十进制.八进制和十六进制. ...

  5. JAVA_SE基础——7.常量&变量

    上一篇,我讲了标识符&关键字    这篇我来解释下变量&常量~~~ 变量与常量这两个概念相信大家都不会感到陌生,在数学中就已经涉及了变量与常量.理解变量与常量,可以举这样一个例子: 例 ...

  6. JAVA_SE基础——5.第一个Java程序HelloWorld&注释的应用

    配置完JDK&环境变量后,我们就可以开始写程序了,那么程序怎么写呢,用什么工具呢,我建议 为了方便学习,我们最好在一个磁盘下建立一个专门的文件来写java程序,比如就在D盘下建立一个名为&qu ...

  7. JAVA_SE基础——4.path的临时配置&Classpath的配置

    这次,我来写下关于path的临时配置的心的 我来说个有可能的实例:如果你去到别人的电脑 又想写代码 又不想改乱别人的path配置的话  再说别人愿意你在别人的电脑上瞎配吗? 那该怎么办呢? 那没问题 ...

  8. JAVA_SE基础——2.环境变量的配置&测试JDK

    哈喽,利用晚上的空余时间再写篇心的~~~  谢谢大家 前一篇文章 JAVA_SE基础--JDK&JRE下载及安装http://blog.csdn.net/thescript_j/article ...

  9. JAVA_SE基础——1.JDK&JRE下载及安装

    这是我学了JAVA来写的第一篇博客: 我首先是在传智播客领了张.毕向东老师的免费JAVA学习光盘来学习! 下面我来教大家安装使用JAVA时候必备的JDK 1.首先上甲骨文公司的官方网站下载JDK的安装 ...

随机推荐

  1. wireshark抓包看ECN

    由于实验需要,要统计ECN信息.为了验证拓扑中是否真的有ECN信息,用了wireshark进行抓包查看. 网上找到的相关有用资料有:http://blog.csdn.net/u011414200/ar ...

  2. webapi下的web请求

    先看webapi提供的服务: [HttpPost] public ResultBaseModel SiteList(SiteModel param) { ResultBaseModel resultM ...

  3. handsontable 合并单元格

    <!DOCTYPE html> <html> <head> <title>handsontable demo</title> <met ...

  4. Json技术使用代码示例

    json格式细节1 JSON(JavaScript Object  Notation)一种简单的数据格式,比xml更轻巧.JSON是JavaScript原生格式,这意味着在JavaScript中处理J ...

  5. 大数据(1):基于sogou.500w.utf8数据的MapReduce程序设计

    环境:centos7+hadoop2.5.2 1.使用ECLIPS具打包运行WORDCOUNT实例,统计莎士比亚文集各单词计数(文件SHAKESPEARE.TXT). ①WorldCount.java ...

  6. eclipse的注释

    版权声明:本文为博主原创文章,转载请注明出处. 如果能帮助你,那我的目的就达到了 Window --> Java --> Code Style --> Code Templates ...

  7. 关于 preHandle 重写和添加参数问题,重写HttpServletRequestWrapper和Filter

    由于 preHandle 中HttpServletRequest 只有setAttribute而没有setParameter 也没有 add 方法 所以是没办法直接添加参数的.从网上查了很多资料,基本 ...

  8. CentOS 远程桌面相关服务安装笔记

    # CentOS 7安装图形界面 sudo yum groupinstall "GNOME Desktop" "Graphical Administration Tool ...

  9. python文件读read()、readline()、readlines()对比

    读取文件的三个方法:read().readline().readlines().均可接受一个变量用以限制每次读取的数据量,但通常不使用.本章目的是分析和总结三种读取方式的使用方法和特点. 一.read ...

  10. Microsoft AI - Custom Vision in C#

    概述 前面一篇 Microsoft AI - Custom Vision 中,我们介绍了 Azure 认知服务中的自定义影像服务:Custom Vision,也介绍了如果通过这个在线服务,可视化的完成 ...