不要投event.error
到Error
,如:
Error(event.error).getStackTrace()
得到一個錯誤的堆棧跟蹤誤差的建設作爲一個字符串時返回錯誤的調用堆棧。請注意,堆棧跟蹤的行號是轉換的行。
相反,調用getStackTrace()
從event.error
,如:
event.error.getStackTrace()
沒有什麼根據您的例子在棧上顯著。
添加遞歸有助於說明問題:
package
{
import flash.display.Sprite;
import flash.events.UncaughtErrorEvent;
public class ExceptionTest extends Sprite
{
public function ExceptionTest()
{
super();
loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler);
recursion();
}
protected function recursion(depth:uint=0):void
{
if (depth == 5)
throw new Error("test");
else
recursion(++depth);
}
protected function uncaughtErrorHandler(event:UncaughtErrorEvent):void
{
trace(event.error.getStackTrace());
}
}
}
...會產生:
Error: test
at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:19]
at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:21]
at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:21]
at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:21]
at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:21]
at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:21]
at ExceptionTest()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:13]
[SWF] Users:jsturges:dev:flash-workspace:X:bin-debug:ExceptionTest.swf - 1,745 bytes after decompression
Error: test
at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:19]
at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:21]
at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:21]
at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:21]
at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:21]
at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:21]
at ExceptionTest()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:13]
如果uncaughtErrorHandler()
改爲你的榜樣鑄造Error
,如:
protected function uncaughtErrorHandler(event:UncaughtErrorEvent):void
{
trace(Error(event.error).getStackTrace());
}
調試器捕獲異常,但是getStackTrace
是投行:
Error: test
at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:19]
at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:21]
at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:21]
at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:21]
at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:21]
at ExceptionTest/recursion()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:21]
at ExceptionTest()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:13]
[SWF] Users:jsturges:dev:flash-workspace:X:bin-debug:ExceptionTest.swf - 1,519 bytes after decompression
Error: Error: test
at ExceptionTest/uncaughtErrorHandler()[/Users/jsturges/dev/flash-workspace/X/src/ExceptionTest.as:26]
也可能有細微差別編組AS1和AS2到支持UncaughtErrorEvent
起始於Flash Player 10.1的AS3。
除此之外,請確保每個ActionScript版本都有適當的調試播放器。在運行時的非調試版本中,Error.getStackTrace()
方法返回null
。
用ASC2以外的東西編譯時工作正常嗎? – 2013-04-18 02:14:15
首先,這個問題放在一起非常糟糕,我不能編輯它使其可讀。很難理解屬於對方的東西,我完全不理解這個問題。其次,你會收到一條錯誤消息。您不會向我們提供代碼,也不會告訴我們代碼中發生錯誤的行。如果在任何try..catch塊之外拋出錯誤,或者發送ErrorEvent對象時沒有註冊偵聽器,則會發生未捕獲的錯誤。你只是說「我的應用程序有錯誤,請幫助我。」我們該怎麼做? – Joetjah 2013-04-18 07:30:45
我很抱歉,我已經重新編輯它。它是否描述清楚? – hooh 2013-04-22 02:20:08