Java 异常总结
Throwablede类是 Java 语言中所有错误或异常的超类。
package com.cwcec.test; class FuShuException extends Exception
{
public FuShuException()
{ } public FuShuException(String msg)
{
super(msg);
}
}
class Person
{
int age;
String name;
public Person(String name,int age)
{
this.name = name;
this.age = age;
} public Person()
{ } public int method(int[] arr,int index) throws FuShuException //throw是语句抛出一个异常。
{
if(arr == null)
{
throw new NullPointerException("数组不能为null");
}
if(index >= arr.length)
{
throw new ArrayIndexOutOfBoundsException("数组访问的下标越界");
}
if (index < 0)
{
throw new FuShuException("数组下标不能为负数"); //throws是方法可能抛出异常的声明
}
return arr[index];
}
} public class FieldDemo
{
public static void main(String[] args) throws FuShuException
{ Person person = new Person("Tom", 50);
int[] arr = {1,2,3};
int rel = person.method(null, 2);
System.out.println(rel); } }
class TestA{
    //compiles fine.we don't need to claim the RuntimeException to be thrown here
    void method(){
        throw new RuntimeException();
    }
}
class TestB{
    void method() throws RuntimeException{
        throw new RuntimeException();
    }  
    void invokeMethod(){
        //compiles fine. we don't need the try-catch clause here
        method();
    }
}
class TestC{  
    //compiles error.we need to claim the Exception to be thrown on the method name
    void method(){
        throw new Exception();
    }
}
class TestD{
    //compiles fine.
    void method() throws Exception{
        throw new Exception();
    }
} 
以下所有的相关异常的特性都不包括RuntimeException及其子类。
2. 假如一个方法在父类中没有声明抛出异常,那么,子类覆盖该方法的时候,不能声明异常。
class TestA{
    void method(){}
}
class TestB extends TestA{  
    //complies error if the method overrided pertaining to the base class doesn't declare throwing exceptions
    void method() throws Exception{
        throw new Exception();
    }
}
 
class TestA{
    void method() throws IOException{}
}
class TestB extends TestA{
    //compiles fine if current method does not throw any exceptions
    void method(){}
}
class TestC extends TestA{
    //compiles fine because InterruptedIOException is inherited from IOException which is thrown by the overrided method of the base class
    void method() throws InterruptedIOException{}
}
class TestD extends TestA{
    //compiles error because Exception thrown by current method is not inherited from IOException which is thrown by the overrided method of the base class
    void method() throws Exception{}
}    
class TestA {
    public TestA() throws IOException {}  
    public TestA(int i) {}
}  
class TestC extends TestA {
    // compiles fine if current constructor doesn't throw anything.
    public TestC() { super(0); }
}  
class TestB extends TestA {
    // compiles fine even if current constructor throws exceptions which don't
    // inherit from exceptions that are thrown by the overrided method of the
    // base class
    // this also means constructors don't conform the inheriting system of JAVA
    // class
    public TestB() throws Exception {}
}  
class ExceptionA extends Exception{
}
class ExceptionB extends Exception{  
}
interface TestA{
    void method() throws ExceptionA;
}
abstract class TestB{
    abstract void method() throws ExceptionB;
}
class TestC extends TestB implements TestA{
    //compiles error
    public void method() throws ExceptionA{}
}
class TestD extends TestB implements TestA{
    //compiles error
    public void method() throws ExceptionB{}
}
class TestE extends TestB implements TestA{
    //compiles error
    public void method() throws ExceptionA,ExceptionB{}
}
class TestF extends TestB implements TestA{
    //compiles fine
    public void method(){}
}  
Java 异常总结的更多相关文章
- 浅谈java异常[Exception]
		学习Java的同学注意了!!! 学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入Java学习交流群,群号码:589809992 我们一起学Java! 一. 异常的定义 在<java编程思想 ... 
- 基础知识《十》java 异常捕捉 ( try catch finally ) 你真的掌握了吗?
		本文转载自 java 异常捕捉 ( try catch finally ) 你真的掌握了吗? 前言:java 中的异常处理机制你真的理解了吗?掌握了吗?catch 体里遇到 return 是怎么处理 ... 
- Java异常体系及分类
		上图是基本的java异常体系结构. 主要分为2大类:Error和Exception 1.Error:描述了Java运行系统中的内部错误以及资源耗尽的情形.应用程序不应该抛出这种类型的对象,一般是由虚拟 ... 
- Java异常之自定义异常
		哎呀,妈呀,又出异常了!俗话说:"代码虐我千百遍,我待代码如初恋". 小Alan最近一直在忙着工作,已经很久没有写写东西来加深自己的理解了,今天来跟大家聊聊Java异常.Java异 ... 
- 第11章 Java异常与异常处理
		1.Java异常简介 1.什么是异常异常出现的时候代码会无法正常运行下去,会产生各种问题2.捕捉异常的作用提早发现异常,方便查找问题,并给出解决方法3.Java中的异常1.Java中所有不正常的类都是 ... 
- java 异常
		1.java异常 2.自定义抛出 3.运行时异常,程序有问题,让使用者可以改' ' 4.return 和 throw的区别 return 符合函数要求的值 throw 有问题的时候用它结束 ... 
- 3.Java异常进阶
		3.JAVA异常进阶 1.Run函数中抛出的异常 1.run函数不会抛出异常 2.run函数的异常会交给UncaughtExceptionhandler处理 3.默认的UncaughtExceptio ... 
- 2.Java异常学习
		1.Java异常的概念 异常的例子 1.除法就是一个需要捕获异常的例子,除数又可能是0 异常处理的基本流程如下 一旦发生异常,就使得程序不按照原来的流程继续的运行下去 a.程序抛出异常 try{ th ... 
- java异常架构图 和几个面试题
		1.java异常架构图 粉红色的是受检查的异常(checked exceptions),其必须被 try{}catch语句块所捕获,或者在方法签名里通过throws子句声明.受检查的异常必须在编译时被 ... 
- 黑马----JAVA异常
		黑马程序员:Java培训.Android培训.iOS培训..Net培训 黑马程序员--JAVA异常 一.JAVA异常有三种语句块:try语句块.catch语句块.finally语句块. 1.try语句 ... 
随机推荐
- 无缘DELPHI的BUG
			有个很简单的小错误,看一眼好象是DELPHI的BUG,结果找了一个晚上,后面才发现出错还是自己造成的原因. CLIENTDATASET.LOCATE ! 以为它工作出问题了,后来仔细比对,原来有个数据 ... 
- Python学习之——Oracle数据库连接
			一.安装Oracle客户端 1.下载对应安装文件,官网地址:http://www.oracle.com/technetwork/database/database-technologies/insta ... 
- pyasn1 安装异常
			初探Scrapy 时,也不知道按照哪里的文档装的,由于安装低版本的pyasn1 后,使用pip uninstall 都 卸不了 步骤如下: 异常: pyasn1-modules has require ... 
- express-session deprecated undefined resave option; provide resave option app.js
			nodejs使用express-session报错 代码如下 app.use(session({ secret: 'hubwiz app', //secret的值建议使用随机字符串 cookie: { ... 
- 20155222 2016-2017-2 《Java程序设计》第3周学习总结
			20155222 2016-2017-2 <Java程序设计>第3周学习总结 教材学习内容总结 要产生对象必须先定义类,类是对象的设计图,对象是类的实例. 数组一旦建立,长度就固定了. 字 ... 
- CF 833 B. The Bakery
			B. The Bakery http://codeforces.com/contest/833/problem/B 题意: 将一个长度为n的序列分成k份,每份的cost为不同的数的个数,求最大cost ... 
- 图论-最短路径 2.Dijkstra算法O (N2)
			2.Dijkstra算法O (N2) 用来计算从一个点到其他所有点的最短路径的算法,是一种单源最短路径算法.也就是说,只能计算起点只有一个的情况. Dijkstra的时间复杂度是O (N2),它不能处 ... 
- js页面动态时间展示
			效果图: 具体代码 js代码 <script type="text/javascript"> var t = null; t = setTimeout(time,100 ... 
- 用Micro:bit做剪刀、石头、布游戏
			剪刀.石头.布游戏大家都玩过,今天我们用Micro:bit建一个剪刀.石头.布游戏! 第一步,起始 当你摇动它时,我们希望the micro:bit选择剪刀.石头.布.尝试创建一个on shake b ... 
- 原生WebGL场景中绘制多个圆锥圆柱
			前几天解决了原生WebGL开发中的一个问题,就是在一个场景中绘制多个几何网格特征不同的模型,比如本文所做的绘制多个圆锥和圆柱在同一个场景中,今天抽空把解决的办法记录下来,同时也附上代码.首先声明,圆柱 ... 
