3131import java .nio .charset .StandardCharsets ;
3232import java .nio .file .Files ;
3333import java .nio .file .Path ;
34- import java .nio . file . Paths ;
34+ import java .text . SimpleDateFormat ;
3535import java .util .Date ;
3636import java .util .Properties ;
3737
@@ -192,9 +192,7 @@ public void startM() {
192192 new AMFactory (
193193 atField .getText (), mtField .getText (), new GUIEventHandler (this ), outField .getText ()
194194 ).work ();
195- } catch (RuntimeException e ) {
196- if (!(e .getCause () instanceof InterruptedException ))
197- throw e ;
195+ } catch (Exception ignored ) {
198196 }
199197 }, "Assigning Task Thread" );
200198 // 在运行任务时按关闭会先终止任务线程
@@ -243,24 +241,35 @@ public void handle(AMEvent event, String msg) {
243241 alert .show ();
244242
245243 } else if (index <= AMEvent .getLastEvent ().getIndex ()) {
246- String message = Processor .MSG_ARR [event .ordinal ()];
244+ String message = Processor .MSG_ARR [event .ordinal ()]; // 事件信息
247245 boolean unexpected = false ;
248246 if (msg != null ) {
249247 message += "\n " ;
250- if (msg .startsWith (AMEvent .ERR_FAILED_TO_CLOSE .toString ()) || event == AMEvent .ERR_UNEXPECTED ) {
248+ boolean fail ;
249+ if ((fail = msg .startsWith (AMEvent .ERR_FAILED_TO_CLOSE .toString ())) || event == AMEvent .ERR_UNEXPECTED ) {
251250 unexpected = true ;
252- message += msg .substring (0 , msg .indexOf ('\n' ,
253- AMEvent .ERR_FAILED_TO_CLOSE .toString ().length () + 2 )); // ERR_FAILED_TO_CLOSE\n_
254- String name = new Date (). toString ();
255- Path path = Paths . get ( LocalURL . JAR_PARENT_PATH , "/" , name , ".txt" ) ;
256- try ( BufferedWriter writer = Files . newBufferedWriter ( path , StandardCharsets . UTF_8 )) {
257- writer . write ( msg );
251+ message += msg .substring (0 , msg .indexOf ('\n' , fail ? // 截断事件栈帧
252+ AMEvent .ERR_FAILED_TO_CLOSE .toString ().length () + 2 : 0 )); // if fail -> ERR_FAILED_TO_CLOSE\n_
253+ String name = new SimpleDateFormat ( "yyyy-MM-dd_hh-mm-ss" ). format ( new Date ()); // 生成文件名
254+ Path path = null ;
255+ try {
256+ path = new File ( LocalURL . JAR_PARENT_PATH + name + ".txt" ). getCanonicalFile (). toPath ( );
258257 } catch (IOException e ) {
259- LogManager .getRootLogger ().error ("Can't write error log out!" );
258+ LogManager .getRootLogger ().error ("Failed to get file name to save: " +
259+ e .getLocalizedMessage ());
260260 }
261- //TODO IOException 测试
262- message += "\n 发生意料之外的异常,已保存到jar路径下的文件中," +
263- "按确定以复制错误信息(建议复制到word中防止丢失),并请到GitHub/Gitee上发issue" ;
261+ if (path != null ) {
262+ try (BufferedWriter writer = Files .newBufferedWriter (path , StandardCharsets .UTF_8 )) {
263+ writer .write (msg );
264+ } catch (IOException e ) {
265+ LogManager .getRootLogger ().error ("Can't write error log out: " +
266+ e .getLocalizedMessage ());
267+ }
268+ message += "\n 发生意料之外的异常,已保存到jar路径下的文件中," +
269+ "按确定以复制错误信息(建议复制到word中防止丢失),并请到GitHub/Gitee上发issue" ;
270+ } else
271+ message += "\n 发生意料之外的异常," +
272+ "按确定以复制错误信息(建议复制到word中防止丢失),并请到GitHub/Gitee上发issue" ;
264273 } else
265274 message += msg ;
266275 }
@@ -274,7 +283,7 @@ public void handle(AMEvent event, String msg) {
274283 alert .close ();
275284 };
276285
277- alert .setEventHandler (handler , null );
286+ alert .setEventHandler (handler , null ); // handler默认为null!
278287 alert .show ();
279288 }
280289 });
0 commit comments