Throwablede类是 Java 语言中所有错误或异常的超类。

两个子类的实例,Error 和 Exception
Error 是 Throwablede 的子类,用于指示合理的应用程序不应该试图捕获的严重问题。
Exceptionde 类及其子类是Throwablede 的一种形式,它指出了合理的应用程序想要捕获的条件。
 
  1. package com.cwcec.test;
  2.  
  3. class FuShuException extends Exception
  4. {
  5. public FuShuException()
  6. {
  7.  
  8. }
  9.  
  10. public FuShuException(String msg)
  11. {
  12. super(msg);
  13. }
  14. }
  15. class Person
  16. {
  17. int age;
  18. String name;
  19. public Person(String name,int age)
  20. {
  21. this.name = name;
  22. this.age = age;
  23. }
  24.  
  25. public Person()
  26. {
  27.  
  28. }
  29.  
  30. public int method(int[] arr,int index) throws FuShuException //throw是语句抛出一个异常。
  31. {
  32. if(arr == null)
  33. {
  34. throw new NullPointerException("数组不能为null");
  35. }
  36. if(index >= arr.length)
  37. {
  38. throw new ArrayIndexOutOfBoundsException("数组访问的下标越界");
  39. }
  40. if (index < 0)
  41. {
  42. throw new FuShuException("数组下标不能为负数"); //throws是方法可能抛出异常的声明
  43. }
  44. return arr[index];
  45. }
  46. }
  47.  
  48. public class FieldDemo
  49. {
  50. public static void main(String[] args) throws FuShuException
  51. {
  52.  
  53. Person person = new Person("Tom", 50);
  54. int[] arr = {1,2,3};
  55. int rel = person.method(null, 2);
  56. System.out.println(rel);
  57.  
  58. }
  59.  
  60. }
 
异常的分类:
1、编译时被检测异常:只要是Exception和其子类,除了特殊子类RuntimeException体系。
      这种问题一旦出现,希望在编译时就进行检测,让这种问题有对应的处理方式。
       这种问题可以针对性的进行处理。
 
2、编译时不检测异常(运行时异常):就是Exception中的RuntimeException和其子类。
      这种问题的发生无法让功能继续,运行无法进行,更多的是因为调用的原因导致的或者内部状态的
      改变而导致的。
      这种问题一般不处理,直接编译通过,在运行时,让调用者调用时程序强制停止。
 
所以自定义异常时,要么继承Exception,要么继承RuntimeException。
 
 throws 和throw的区别:
1、throws使用在函数上,是方法可能抛出异常的声明,
      throw使用在函数内,表示抛出一个异常;
2、throws抛出的是异常类,可以抛多个,用逗号隔开,
      throw抛出的是异常对象,只能有一个。
 
--------------------------------------------------------------------------------------------------------------------------------------
1. RuntimeException与Exception, Error不同点: 当方法体中抛出非RuntimeException(及其子类)时,方法名必须声明抛出的异常;但是当方法体中抛出RuntimeException(包括RuntimeException子类)时,方法名不必声明该可能被抛出的异常,即使声明了,JAVA程序在某个调用的地方,也不需要try-catch从句来处理异常。
  1. class TestA{
  2. //compiles fine.we don't need to claim the RuntimeException to be thrown here
  3. void method(){
  4. throw new RuntimeException();
  5. }
  6. }
  7. class TestB{
  8. void method() throws RuntimeException{
  9. throw new RuntimeException();
  10. }
  11.  
  12. void invokeMethod(){
  13. //compiles fine. we don't need the try-catch clause here
  14. method();
  15. }
  16. }
  17. class TestC{
  18.  
  19. //compiles error.we need to claim the Exception to be thrown on the method name
  20. void method(){
  21. throw new Exception();
  22. }
  23. }
  24. class TestD{
  25. //compiles fine.
  26. void method() throws Exception{
  27. throw new Exception();
  28. }
  29. }

以下所有的相关异常的特性都不包括RuntimeException及其子类。

2. 假如一个方法在父类中没有声明抛出异常,那么,子类覆盖该方法的时候,不能声明异常。

  1. class TestA{
  2. void method(){}
  3. }
  4. class TestB extends TestA{
  5.  
  6. //complies error if the method overrided pertaining to the base class doesn't declare throwing exceptions
  7. void method() throws Exception{
  8. throw new Exception();
  9. }
  10. }
3. 假如一个方法在父类中声明了抛出异常,子类覆盖该方法的时候,要么不声明抛出异常,要么声明被抛出的异常继承自它所覆盖的父类中的方法抛出的异常。
  1. class TestA{
  2. void method() throws IOException{}
  3. }
  4. class TestB extends TestA{
  5. //compiles fine if current method does not throw any exceptions
  6. void method(){}
  7. }
  8. class TestC extends TestA{
  9. //compiles fine because InterruptedIOException is inherited from IOException which is thrown by the overrided method of the base class
  10. void method() throws InterruptedIOException{}
  11. }
  12. class TestD extends TestA{
  13. //compiles error because Exception thrown by current method is not inherited from IOException which is thrown by the overrided method of the base class
  14. void method() throws Exception{}
  15. }
4. 构造器不遵循上述规则,因为构造器不遵循JAVA的覆盖和重载规则。
  1. class TestA {
  2. public TestA() throws IOException {}
  3.  
  4. public TestA(int i) {}
  5. }
  6.  
  7. class TestC extends TestA {
  8. // compiles fine if current constructor doesn't throw anything.
  9. public TestC() { super(0); }
  10. }
  11.  
  12. class TestB extends TestA {
  13. // compiles fine even if current constructor throws exceptions which don't
  14. // inherit from exceptions that are thrown by the overrided method of the
  15. // base class
  16. // this also means constructors don't conform the inheriting system of JAVA
  17. // class
  18. public TestB() throws Exception {}
  19. }
5. 当一个类继承某个类,以及实现若干个接口,而被继承的类与被实现的接口拥有共同的方法,并且该方法被覆盖时,它所声明抛出的异常必须与它父类以及接口一致。
  1. class ExceptionA extends Exception{
  2. }
  3. class ExceptionB extends Exception{
  4.  
  5. }
  6. interface TestA{
  7. void method() throws ExceptionA;
  8. }
  9. abstract class TestB{
  10. abstract void method() throws ExceptionB;
  11. }
  12. class TestC extends TestB implements TestA{
  13. //compiles error
  14. public void method() throws ExceptionA{}
  15. }
  16. class TestD extends TestB implements TestA{
  17. //compiles error
  18. public void method() throws ExceptionB{}
  19. }
  20. class TestE extends TestB implements TestA{
  21. //compiles error
  22. public void method() throws ExceptionA,ExceptionB{}
  23. }
  24. class TestF extends TestB implements TestA{
  25. //compiles fine
  26. public void method(){}
  27. }

Java 异常总结的更多相关文章

  1. 浅谈java异常[Exception]

    学习Java的同学注意了!!! 学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入Java学习交流群,群号码:589809992 我们一起学Java! 一. 异常的定义 在<java编程思想 ...

  2. 基础知识《十》java 异常捕捉 ( try catch finally ) 你真的掌握了吗?

    本文转载自  java 异常捕捉 ( try catch finally ) 你真的掌握了吗? 前言:java 中的异常处理机制你真的理解了吗?掌握了吗?catch 体里遇到 return 是怎么处理 ...

  3. Java异常体系及分类

    上图是基本的java异常体系结构. 主要分为2大类:Error和Exception 1.Error:描述了Java运行系统中的内部错误以及资源耗尽的情形.应用程序不应该抛出这种类型的对象,一般是由虚拟 ...

  4. Java异常之自定义异常

    哎呀,妈呀,又出异常了!俗话说:"代码虐我千百遍,我待代码如初恋". 小Alan最近一直在忙着工作,已经很久没有写写东西来加深自己的理解了,今天来跟大家聊聊Java异常.Java异 ...

  5. 第11章 Java异常与异常处理

    1.Java异常简介 1.什么是异常异常出现的时候代码会无法正常运行下去,会产生各种问题2.捕捉异常的作用提早发现异常,方便查找问题,并给出解决方法3.Java中的异常1.Java中所有不正常的类都是 ...

  6. java 异常

    1.java异常 2.自定义抛出 3.运行时异常,程序有问题,让使用者可以改' ' 4.return  和  throw的区别 return 符合函数要求的值    throw  有问题的时候用它结束 ...

  7. 3.Java异常进阶

    3.JAVA异常进阶 1.Run函数中抛出的异常 1.run函数不会抛出异常 2.run函数的异常会交给UncaughtExceptionhandler处理 3.默认的UncaughtExceptio ...

  8. 2.Java异常学习

    1.Java异常的概念 异常的例子 1.除法就是一个需要捕获异常的例子,除数又可能是0 异常处理的基本流程如下 一旦发生异常,就使得程序不按照原来的流程继续的运行下去 a.程序抛出异常 try{ th ...

  9. java异常架构图 和几个面试题

    1.java异常架构图 粉红色的是受检查的异常(checked exceptions),其必须被 try{}catch语句块所捕获,或者在方法签名里通过throws子句声明.受检查的异常必须在编译时被 ...

  10. 黑马----JAVA异常

    黑马程序员:Java培训.Android培训.iOS培训..Net培训 黑马程序员--JAVA异常 一.JAVA异常有三种语句块:try语句块.catch语句块.finally语句块. 1.try语句 ...

随机推荐

  1. RuntimeError: Cannot run in multiple processes: IOLoop instance has already been initialized. You cannot call IOLoop.instance() before calling start_processes()

    解决方法: settings中的debug改为false,或者注释掉 参照: https://stackoverflow.com/questions/32521122/cannot-run-in-mu ...

  2. ARM设备树

    学习目标:学习设备树相关内容: 一.概念 在Linux 2.6中,ARM架构的板极硬件细节过多地被硬编码在arch/arm/plat-xxx和arch/arm/mach-xxx,在kernel中存在大 ...

  3. 20155224 实验一《Java开发环境的熟悉》实验报告

    实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Eclipse 编辑.编译.运行.调试Java程序. 实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版) ...

  4. Win10系统下VirtualBox虚拟机初体验

    在接触本次的VirtualBox之前,我在大一下学期参加李冬冬老师的选修课中学习过VMware,并使用VMware进行过一些计算机病毒之类的实验.但是,使用虚拟机模拟其他不同操作系统这次是第一次,因此 ...

  5. 20155322 2016-2017-2 《Java程序设计》 第一周学习总结

    20155322 2016-2017-2 <Java程序设计> 第一周学习总结 教材学习内容总结 本周学习内容的主要是: 一.浏览教材,根据自己的理解每章提出一个问题. 在浏览教材后,我提 ...

  6. BZOJ054_移动玩具_KEY

    题目传送门 这道题我写IDA*写挂了,TLE+WA,只AC了两个点. 这道题标算BFS+状态压缩. code: /******************************************* ...

  7. 成都Uber优步司机奖励政策(4月14日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  8. #333. 【NOIP2017】宝藏

    #333. [NOIP2017]宝藏 http://uoj.ac/problem/333 1.错误的$n^42^n$做法: dp[s]表示当前的点集为s,然后从这些点中选一个做起点i,然后枚举边,然后 ...

  9. Spring学习(九)-----Spring bean配置继承

    在 Spring,继承是用为支持bean设置一个 bean 来分享共同的值,属性或配置. 一个子 bean 或继承的bean可以继承其父 bean 的配置,属性和一些属性.另外,子 Bean 允许覆盖 ...

  10. Maven学习(七)-----Maven添加远程仓库

    Maven添加远程仓库 默认情况下,Maven从Maven中央仓库下载所有依赖关系.但是,有些库丢失在中央存储库,只有在Java.net或JBoss的储存库远程仓库中能找到. 1. Java.net资 ...