日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 抛出异常 返回值_java通过抛异常来返回提示信息

發布時間:2025/3/20 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 抛出异常 返回值_java通过抛异常来返回提示信息 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

結論:

如果把通過拋異常的方式得到提示信息,可以使用java.lang.Throwable中的構造函數:

public Throwable(String message) {

fillInStackTrace();

detailMessage = message;

}

public Throwable(String message, Throwable cause) {

fillInStackTrace();

detailMessage = message;

this.cause = cause;

}

原因及代碼示例:

1、通過java.lang.Throwable中的Constructs

publicThrowable(Throwable cause) {

fillInStackTrace();

detailMessage= (cause==null ? null: cause.toString());this.cause =cause;

}

在輸出時獲取的detailMessage是:

exception.ProcessorException: java.lang.IllegalArgumentException: Failt to process

Exception信息的輸出:

exception.ProcessorException: exception.ProcessorException: java.lang.IllegalArgumentException: Failt to process

code:

packageexception;/*2015-8-22*/

public classExceptionChain {public static voidmain(String[] args) {

Business business= newBusiness();try{

business.doBusiness();

}catch(ProcessorException e) {

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

System.out.println("e:\n" +e);

}

}

}classBusiness {public void doBusiness() throwsProcessorException {try{

process1();

}catch(Exception e) {throw newProcessorException(e);//throw new ProcessorException(e.getMessage(), e);//throw new ProcessorException(e.getMessage());

}

}private void process1() throwsProcessorException {try{

process2();

}catch(Exception e) {//throw new ProcessorException(e.getMessage(), e);//throw new ProcessorException(e.getMessage());

throw newProcessorException(e);

}

}private voidprocess2() {throw new IllegalArgumentException("Failt to process");

}

}class ProcessorException extendsException {private static final long serialVersionUID = -4270191862690602942L;publicProcessorException(Throwable cause) {super(cause);

}publicProcessorException(String message) {super(message);

}publicProcessorException(String message, Throwable cause) {super(message, cause);

}

}

2、通過java.lang.Throwable中的Constructs

publicThrowable(String message) {

fillInStackTrace();

detailMessage=message;

}

/*** Fills in the execution stack trace. This method records within this

* Throwable object information about the current state of

* the stack frames for the current thread.

*

*@returna reference to this Throwable instance.

*@seejava.lang.Throwable#printStackTrace()*/

public synchronized native Throwable fillInStackTrace();

在輸出時獲取的detailMessage是:

Failt to process

Exception信息的輸出:

exception.ProcessorException: Failt to process

code:

把上面示例代碼中throw new ProcessorException(e.getMessage());注釋去掉,把?// throw new ProcessorException(e);注釋

3、通過java.lang.Throwable中的Constructs:

publicThrowable(String message, Throwable cause) {

fillInStackTrace();

detailMessage=message;this.cause =cause;

}

在輸出時獲取的detailMessage是:

Failt to process

Exception信息的輸出:

exception.ProcessorException: Failt to process

code:

操作方式與2相同

總結

以上是生活随笔為你收集整理的java 抛出异常 返回值_java通过抛异常来返回提示信息的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。