关于Java异常的处理的一个小问题

当double d1 = Double.parseDouble(args[0]);发生异常时,我想在catch里面得到发生异常的这句话......

就是通过使用传进来的 e 杂个能得到发生异常的"double d1 = Double.parseDouble(args[0]);"这句话,以及这句话发生的本JAVA文件名,JAVA文件中这句话的行号等呢?

public static void main(String[] args) {
try{
if (3 != args.length){
System.out.println("Usage: TestArray1 " + "\"n1\""+" \"op\" " +"\"n2\"");
System.exit(-1);
}
double d1 = Double.parseDouble(args[0]);
}catch (Exception e) {
//?????在这里杂个写呢?
}
}

可以从e.getStackTrace();返回的字符串中解析出你要的信息(因为没有直接的接口):

[code="java"] public static void main(String[] args) throws UnsupportedEncodingException, ClassNotFoundException {
try {
double d1 = Double.parseDouble("a");
} catch (Exception e) {
StackTraceElement[] trace = e.getStackTrace();
String firstException = trace[trace.length-1].toString();
String[] strList = firstException.split(".main");
String packageName = strList[0];
strList = strList[1].split(":");
String fileName = strList[0].substring(1);
String countNum = strList[1].substring(0, strList[1].length()-1);

        System.out.println("packageName: " + packageName);
        System.out.println("fileName: " + fileName);
        System.out.println("countNum: " + countNum);
    }
}[/code]

返回结果:
packageName: com.robert.base.TestClass
fileName: TestClass.java
countNum: 26

不过按照“effective java”说的,这是一种不好的获取信息的方式,最好不要采用。

System.out.println(e.toString());

把异常堆栈打出来?里面信息就很全了。。。。
当前执行文件名可以这样
this.class.getName()

[code="java"]

public class ExceptionParser {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    try {

        double d1 = Double.parseDouble(args[0]);
    } catch (Exception e) {
        // ?????在这里杂个写呢?

        StackTraceElement[] st = e.getStackTrace();
        StackTraceElement s = st[st.length-1];
        System.out.println("异常发生的文件名为:" + s.getFileName() );
        System.out.println("异常发生的类名为:" + s.getClassName() );
        System.out.println("异常发生的方法名为:" + s.getMethodName() );
        System.out.println("异常发生的行号为:" + s.getLineNumber() );

    }
}

}

[/code]

[code="java"]

public class ExceptionParser extends classStatus{

//String classPath = "/src/ExceptionParser.java";
public ExceptionParser() {
}

public static void main(String[] args) {   
    // TODO Auto-generated method stub   
    ExceptionParser test = new ExceptionParser();
    try {                
        double d1 = Double.parseDouble("a");   
    } catch (Exception e) {   
        // ?????在这里杂个写呢?   
        StackTraceElement[] st = e.getStackTrace();   
        StackTraceElement s = st[st.length-1];
        System.out.println("异常发生的文件名为:" + s.getFileName()); 
        System.out.println("异常发生的类名为:" + s.getClassName());   
        System.out.println("异常发生的方法名为:" + s.getMethodName());   
        System.out.println("异常发生的行号为:" + s.getLineNumber()); 
        //System.getProperty("user.dir")+"/src/ExceptionParser.java"
        //我的src是java的根目录,自己相应修改一下
        System.out.println("异常发生的行:"+readF(s.getLineNumber(),System.getProperty("user.dir")+"/src/ExceptionParser.java"));
    }   
}   

}

[/code]
[code="java"]
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

public class classStatus {

public static String readF(int errno,String childClassPath){
    String line = null;
    FileInputStream fin = null;
    BufferedReader in = null;
    try {
        int no = 1;//行数
        fin = new FileInputStream(childClassPath);
        in = new BufferedReader(new java.io.InputStreamReader(fin,"UTF-8"));
        line = in.readLine();
        no++;
        while(line!=null){
            line = in.readLine();
            if(no==errno){
                break;
            }
            no++;
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally{
        if(in!=null){
            try {
                in.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(fin!=null){
            try {
                fin.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    return line;
}

}
[/code]
结果是
异常发生的类名为:ExceptionParser
异常发生的方法名为:main
异常发生的行号为:12
F:\companyWorkspace\paoding
异常发生的行: double d1 = Double.parseDouble("a");

catch (Exception e) {

        e.printStackTrace();
    }