ゆの in Javaって何ですかww?
static { Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { public void uncaughtException(Thread t, Throwable e) { if(e instanceof AssertionError) System.out.println("ひだまりスケッチ365 " + e.getMessage()); } }); }
UncaughtExceptionHandlerを使って細工するというのは面白いですね。ただ例外使うなら「ひだまりスケッチ365」という文字列もスタックトレースから拾った方がもっとバカバカしくて面白いのでは?と思ったので私も書いてみました。
public class Yuno { public static void main(String[] args) { new Yuno( X / _ / X < _,"来週も見てくださいね!" );} static int X = 1; static int _ = 1; static { Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { public void uncaughtException(Thread t, Throwable e) { System.out.print(e.getStackTrace()[0].getMethodName()); System.out.print(" "); System.out.print(e.getMessage()); } }); } static int ひだまりスケッチx365(boolean b, String str) { throw new RuntimeException(str); } public Yuno(boolean b, String str) { try { Yuno.class.getDeclaredMethods()[0].invoke(null,b,str); } catch(Exception e) { throw (RuntimeException)e.getCause(); } } }
リフレクションは不要ですが「ひだまりスケッチ365」と書くのは一度だけにしたかったので無理やり使いました。
実行結果
ひだまりスケッチx365 来週も見てくださいね!
これはxaucia909さんの、X / _ / X < _ の演算結果と"来週も見てくださいね!"を引数にしているアイディアを拝借しています。
http://d.hatena.ne.jp/xaucia909/20080711/1215760385
他には
クラス名もスタックトレースに入っているので、それも使って、さらに「来週も見てくださいね」をメソッドにしてみる方法
public class ひだまりスケッチ { public static void main(String[] args) { if ( X / _ / X < 来週も見てくださいね ("×365 "));} static { Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { public void uncaughtException(Thread t, Throwable e) { System.out.print(e.getStackTrace()[0].getClassName()); System.out.print(e.getMessage()); System.out.print(e.getStackTrace()[0].getMethodName()); } }); } static int X = 1; static int _ = 1; static int 来週も見てくださいね(String str) { throw new RuntimeException(str); } }
「!」をメソッド名に使えないのと、シンボル2回書く必要があるのが残念。
何やら面白くて変なのが色々あるみたいですね〜。
追記
正式名称は「ひだまりスケッチ×365 」なんですね。しかしJavaでは×(バツ)をクラス名やメソッド名には使えないようなのでx(小文字エックス)で代用…、うむむむ…。
さらに追記
Tclのあまりのスマートさに泣いた。