Skip to content

Instantly share code, notes, and snippets.

@eliantor
Created April 9, 2012 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliantor/2346353 to your computer and use it in GitHub Desktop.
Save eliantor/2346353 to your computer and use it in GitHub Desktop.
try {
URL url=new URL("foo");
} catch (MalformedURLException e) {
//throw new RuntimeException(e);
sneak(e);
}
StackTrace:
Exception in thread "main" java.net.MalformedURLException: no protocol: foo
at java.net.URL.<init>(URL.java:567)
at java.net.URL.<init>(URL.java:464)
at java.net.URL.<init>(URL.java:413)
at foo.Test.main(Test.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
invece di:
try {
URL url=new URL("foo");
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
Exception in thread "main" java.lang.RuntimeException: java.net.MalformedURLException: no protocol: foo
at foo.Test.main(Test.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.net.MalformedURLException: no protocol: foo
at java.net.URL.<init>(URL.java:567)
at java.net.URL.<init>(URL.java:464)
at java.net.URL.<init>(URL.java:413)
at foo.Test.main(Test.java:15)
... 5 more
public final class Util{
/**
* lancia l'eccezione t anche se non è dichiarata nel metodo
* ma senza farne il wrapparla in una RuntimeException
*/
public static RuntimeException throwUndeclared(Throwable t){
if(t == null) throw new NullPointerException("t");
Util.<RuntimeException>throw0(t);
return null;
}
private static <T extends Throwable> void throw0(Throwable t) throws T {
throw (T)t;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment