由于在File类中引用了./..类表示路径文件,在调用getParentFile()后编译通过,运行时提示:
Exception in thread "main" java.lang.NullPointerException ....
为此定义了一个自定义异常来明确异常类,准备再捕获后再做转换处理,发现自定义异常没有响应,何故?程序如下:import java.io.File;
import java.io.IOException;public class FileTest1{
public static void main(String[] args) throws IOException,PathExpressErr{
File file = new File(".");
try{
System.out.println("file.getParentFile().isDirectory()="+file.getParentFile().isDirectory());
System.out.println("file.getParentFile().isFile()="+file.getParentFile().isFile());
System.out.println("file.getParentFile().exist()="+file.getParentFile().exists());
throw new PathExpressErr();
}catch(PathExpressErr e){
e.getMessage();
}
}
}
class PathExpressErr extends Exception{
String[] pathname ; public PathExpressErr(){
pathname = new String[]{".",".."};
}
public String getMessage(){
String pathStr = "";
for (int i=0; i<pathname.length; i++){
pathStr = pathname[i]+" "+pathStr;
}
return "表示为"+pathStr+"的路径不能被识别,请进行转换!";
}
}
Exception in thread "main" java.lang.NullPointerException ....
为此定义了一个自定义异常来明确异常类,准备再捕获后再做转换处理,发现自定义异常没有响应,何故?程序如下:import java.io.File;
import java.io.IOException;public class FileTest1{
public static void main(String[] args) throws IOException,PathExpressErr{
File file = new File(".");
try{
System.out.println("file.getParentFile().isDirectory()="+file.getParentFile().isDirectory());
System.out.println("file.getParentFile().isFile()="+file.getParentFile().isFile());
System.out.println("file.getParentFile().exist()="+file.getParentFile().exists());
throw new PathExpressErr();
}catch(PathExpressErr e){
e.getMessage();
}
}
}
class PathExpressErr extends Exception{
String[] pathname ; public PathExpressErr(){
pathname = new String[]{".",".."};
}
public String getMessage(){
String pathStr = "";
for (int i=0; i<pathname.length; i++){
pathStr = pathname[i]+" "+pathStr;
}
return "表示为"+pathStr+"的路径不能被识别,请进行转换!";
}
}
catch (Exception e) {
throw new PathExpressErr();
}
另外有一个SecurityException异常,这个安全异常指在哪些情况下出现呢?对这个异常不解?
你应该先了解一下java的异常机制,如checked/unchecked exception
刚才又看了一遍,又体会了一层,非常感谢kingfish