1.如何獲取exception的異常信息
--1.通過RAISE彈出框(調試時使用)
--2.通過sqlcode , sqlerrm 這兩個內置變量來查看,例如:
DECLARE
--聲明異常
some_kinds_of_err EXCEPTION; -- Exception to indicate an error condition
v_ErrorCode NUMBER; -- Variable to hold the error message code
v_ErrorText VARCHAR2(200); -- Variable to hold the error message text
BEGIN
--。
--拋出異常
IF ( 。 ) THEN --(括號內填拋出異常的條件)
RAISE some_kinds_of_err;
END IF;
--。
EXCEPTION
--捕捉異常
WHEN some_kinds_of_err THEN
/* do something to Handler the errors */
null;
--捕捉其他異常,并獲得 捕獲異常的內容
WHEN OTHERS THEN
v_ErrorCode := SQLCODE;
v_ErrorText := SUBSTR(SQLERRM, 1, 200);
-- Note the use of SUBSTR here.
dbms_*_line(v_ErrorCode || '::'||v_ErrorText);
END;
/**
sqlcode 就是錯誤代碼
sqlerrm 就是sql錯誤信息。注意用substr來截取,否則輸出很難看。
**/
*中如何獲取 控制臺的輸出信息,錯誤信息,和異常信息
很簡單的,如果你查api文檔會發現類System有個“字段摘要”,很容易發現有個out,
它返回static PrintStream,還會發現System有個方法是static void setOut(PrintStream out)
重新分配“標準”輸出流。 再點擊PrintStream,很明顯它是OutputStream
的子類 解決如下
輸出流重定向
import *.*;
public class IO2File {
public static void main(String[] args) throws IOException {
File f=new File("*");
*NewFile();
FileOutputStream fileOutputStream = new FileOutputStream(f);
PrintStream printStream = new PrintStream(fileOutputStream);
*(printStream);
*n("默認輸出到控制臺的這一句,輸出到了文件 *");
}
}
* 獲取異常信息
我覺得你寫的是對的
StackTraceElement[] st = *ckTrace();
for (StackTraceElement stackTraceElement : st) {
String exclass = *ssName();
String method = *hodName();
*n(exclass);
*n(method);
}
}
這個就是整個異常拋出的棧結構啊
*怎么獲取exception信息
通過如下代碼:
public static String (Exception ex){
String sOut = "";
StackTraceElement[] trace = *ckTrace();
for (StackTraceElement s : trace) {
sOut += "\tat " + s + "\r\n";
}
return sOut;
}
擴展資料:
注意事項
oracle存儲過程,可以通過sqlcode 獲取異常編碼、通過sqlerrm獲取異常信息。
例子:
create or replace procedure write2blob(p_id in number, --id
p_blob_data in blob,
p_msg out varchar2) --數據
is
v_lobloc blob; --目標blob
v_blob_data blob; --作為接受參數的字段,參數變量不能直接拿來賦值
v_amount binary_integer; --總長度
v_sub_length binary_integer; --一次讀取的最大長度,不超過32766
v_sub_blob blob; --一次讀取的子串
v_offset binary_integer; --游標
v_err_msg varchar2(1000);
v_id binary_integer; --要修改或新增的記錄ID
v_temp binary_integer; --臨時變量用于判斷是否有v_id對應的記錄begin
v_amount := length(p_blob_data);
v_blob_data := p_blob_data;
v_sub_length := 32767;
v_offset := 1;
v_id := p_id;
v_temp := 0; --execute immediate v_query_string into v_lobloc;
select count(1) into v_temp from a where id = v_id; --查詢是否有v_id對應的記錄,并且賦值給v_te/*注意:無論是修改還是新增,blob字段都需要用empty_blob()進行初始化,否則后邊的blob內容,不能寫進表里面。
轉載請注明出處華閱文章網 » 如何獲取異常信息簡短內容