易截截图软件、单文件、免安装、纯绿色、仅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


相关文档:

java base64编码和解码

import java.io.IOException;
public class test {

/**
* 编码
* @param filecontent
* @return String
*/
public static String encode(byte[] bstr){
return new sun.misc.BASE64Encoder().encode(bstr);
}
/**
* 解码
* @param filecontent
* @return string
*/
public static ......

简明Java笔记

配置java环境变量:     JAVA_HOME:配置JDK的目录     CLASSPATH:指定到哪里去找运行时需要用到的类代码(字节码)     PATH:指定可执行程序的位置     LINUX系统(在" .bash_profile "下的环境变量设置)        ......

Java解析网络数据流的三种特殊方法

一、UTF8转换成GB2312
       
当我们在基于HTTP协议的JSP或Servlet的应用中获取数据或发送请求时,JVM会把输送的数据编码成UTF8格式。如果我们直接从HTTP流中提取中文数据,提取的结果为“????”(可能更多问号),为转换成我们能够理解的中文字符,我们需要把UTF8转换成 ......

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

一、Servlet过滤器的概念:
***************************************************************************************
Servlet过滤器是在Java Servlet规范2.3中定义的,它能够对Servlet容器的请求和响应对象进行检查和修改。   
Servlet过滤器本身并不产生请求和响应对象,它只能提供过滤作用。Servlet过期能 ......

AjaxSwing 3.0发布 可将Java Swing转化为Ajax

AjaxSwing是一个专为Java Swing应用 程序打造的Web开发平台。使用AjaxSwing,可以使为Java桌面应用程序转为Web应用。 AjaxSwing可以被看作是一个Java到HTML的转换器,其程序可以在运行时创建被浏览器解析的HTML和 JavaScript。另外,使用AjaxSwing无需对程序和业务逻辑进行大幅修改修改,它允许开发者使用标准Swing组件来实 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号