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
相关文档:
用关键字new生成对象:这是最常用的一种方式,例如 new String("hello")用new生成对象的特点是,这个对象的类必须在编译时就在classpath中,如果没有特别的理由和要求,这是我们生成一个对象的第一选择;
Class.forName(String className)。例如Class c = Class.forName("com.company.jdbc.Driver");这种方法的特点是在编 ......
java利用smslib发送短信.
自己写一个小程序,我在java1.6.0_10;smslib-v3.4.5下运行成功. 可以我的资源里面下载.
http://hi.csdn.net/link.php?url=http://yangzl0123.download.csdn.net
,主要是以下几个类.
Level_Final_Serial.java:串口底层操作
Serial_For_Smslib.java:对Level_Final_Serial的进一步封装,可以直接发 ......
本文的预定读者首先要对j2ee有所了解,熟悉xml,tomcat等基本内容,本文主要是简单介绍一下web服务的基本内容,怎样在java web开发中构建SOAP服务:
一、
SOAP(Simple Object Access
Protocol)简单对象访问协议,要了解SOAP,首先就需要了解分布式计算的由来,随着下一代的分布式计算体系web服务的出现,SOAP成 ......
一、UTF8转换成GB2312
当我们在基于HTTP协议的JSP或Servlet的应用中获取数据或发送请求时,JVM会把输送的数据编码成UTF8格式。如果我们直接从HTTP流中提取中文数据,提取的结果为“????”(可能更多问号),为转换成我们能够理解的中文字符,我们需要把UTF8转换成 ......
1.java static inner class 和 non-static inner class的区别?
2.请写出一个singleton模式的class.
你如果写出下面的2种样式,我会问你:请问你如何在同一个jvm中并且在同一个classLoader中得到它的多个实例?(请不要奇怪)
样列1:
public class Singleton {
private final static Singleton instance= ......