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
相关文档:
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 ......
动态代理:
public interface Qingke {
void qk();
}
public class dsz implements Qingke{
public void qk() {
System.out.print("dsz qk");
}
}
public class Secretary implements InvocationHandler {
private Object pro;
private dsz dsz;
public Obj ......
java中删除目录事先要删除目录下的文件或子目录。用递归就可以实现。
public void del(String filepath) throws IOException{
File f = new File(filepath);//定义文件路径
if(f.exists() && f.isDirectory()){//判断是文件还是目录
if( ......
【一】基于字节的输入流
值得注意的地方有:
①Level 2的输入流,大多数都会指明数据源的形式:例如ByteArray,File,Piped
②Level 3的输入流,则不会出现具体的数据源名字,而是以功能取代:例如Buffered,LineNumber
所以说Level 3的输入流是对Level 2输入流的“封装和过滤”。实际上Level 2的输入流,� ......