易截截图软件、单文件、免安装、纯绿色、仅160KB

JAVA异常总结 继承

以下是对JAVA异常的继承机制的一些总结。
1. RuntimeException与Exception, Error不同点: 当方法体中抛出非RuntimeException(及其子类)时,方法名必须声明抛出的异常;但是当方法体中抛出RuntimeException(包括RuntimeException子类)时,方法名不必声明该可能被抛出的异常,即使声明了,JAVA程序在某个调用的地方,也不需要try-catch从句来处理异常。
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();
}
}
 
3. 假如一个方法在父类中声明了抛出异常,子类覆盖该方法的时候,要么不声明抛出异常,要么声明被抛出的异常继承自它所覆盖的父类中的方法抛出的异常。
class TestA{
void method() throws RuntimeException{}
}
class TestB extends TestA{
//compiles fine if current method does not throw any exceptions
void method(){}
}
class TestC extends TestA{
//compiles fine because NullPointerException is inherited from RuntimeException which is thrown by the overrided method of the base class
void method() throws NullPointerException{}
}
class TestD extends TestA{
//compiles error because Exception thrown by current method is not inherited from RuntimeException which is thr


相关文档:

使用SOAP开发java web服务 Axis开发方案

本文的预定读者首先要对j2ee有所了解,熟悉xml,tomcat等基本内容,本文主要是简单介绍一下web服务的基本内容,怎样在java web开发中构建SOAP服务:
 一、
SOAP(Simple Object Access
Protocol)简单对象访问协议,要了解SOAP,首先就需要了解分布式计算的由来,随着下一代的分布式计算体系web服务的出现,SOAP成 ......

在Java程序中截获控制台输出

     本文的目标是设计一个基于Swing的JTextArea显示控制台输出。此期间,我们还将讨论一些和Java管道流(PipedInputStream和PipedOutputStream)有关的注意事项。最后还要创建一个能够捕获和显示其他程序(可以是非Java的程序)控制台输出的简单程序。
一、Java管道流
     要在文本 ......

JAVA JDBC 简单分页

        /**
          *需要传入 pageNo 当前页
          *             pageSize一页显示的条数
      ......

浅析Java的“克隆”方法 转贴

ZT:http://javahy.javaeye.com/blog/384871
  Java语言的一个优点就是取消了指针的概念,但也导致了许多程序员在编程中常常忽略了对象与引用的区别,本文会试图澄清这一概念。并且由于Java不能通过简单的赋值来解决对象复制的问题,在开发过程中,也常常要要应用clone()方法来复制对象。本文会让你了解什么是影子clone ......

JAVA WEB学习笔记(四)-Servlet过滤器

一、Servlet过滤器的概念:
***************************************************************************************
Servlet过滤器是在Java Servlet规范2.3中定义的,它能够对Servlet容器的请求和响应对象进行检查和修改。   
Servlet过滤器本身并不产生请求和响应对象,它只能提供过滤作用。Servlet过期能 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号