Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9074037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:32:53+00:00 2026-06-16T18:32:53+00:00

Can someone explain this code? public class SneakyThrow { public static void sneakyThrow(Throwable ex)

  • 0

Can someone explain this code?

public class SneakyThrow {


  public static void sneakyThrow(Throwable ex) {
    SneakyThrow.<RuntimeException>sneakyThrowInner(ex);
  }

  private static <T extends Throwable> T sneakyThrowInner(Throwable ex) throws T {
    throw (T) ex;
  }



  public static void main(String[] args) {
    SneakyThrow.sneakyThrow(new Exception());
  }


}

It may seems strange, but this doesn’t produce a cast exception, and permits to throw a checked exception without having to declare it in the signature, or to wrap it in an unchecked exception.

Notice that neither sneakyThrow(...) or the main are declaring any checked exception, but the output is:

Exception in thread "main" java.lang.Exception
    at com.xxx.SneakyThrow.main(SneakyThrow.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

This hack is used in Lombok, with the annotation @SneakyThrow, which permits to throw checked exceptions without declaring them.


I know it has something to do with type erasure, but i’m not sure to understand every part of the hack.


Edit:
I know that we can insert an Integer in a List<String> and that checked/unchecked exceptions distinction is a compile time feature.

When casting from a non-generic type like List to a generic type like List<XXX> the compiler produces a warning. But it’s less common to cast to a generic type directly like (T) ex in the above code.

If you want, the part that seems strange for me is that I understand that inside the JVM a List<Dog> and List<Cat> looks the same, but the above code seems to mean that finally we can also assign a value of type Cat to a variable of type Dog or something like that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T18:32:54+00:00Added an answer on June 16, 2026 at 6:32 pm

    If you compile it with -Xlint you’ll get a warning:

    c:\Users\Jon\Test>javac -Xlint SneakyThrow.java
    SneakyThrow.java:9: warning: [unchecked] unchecked cast
        throw (T) ex;
                  ^
      required: T
      found:    Throwable
      where T is a type-variable:
        T extends Throwable declared in method <T>sneakyThrowInner(Throwable)
    1 warning
    

    That’s basically saying “This cast isn’t really checked at execution time” (due to type erasure) – so the compiler reluctantly assumes you’re doing the right thing, knowing that it won’t actually be checked.

    Now it’s only the compiler which cares about checked and unchecked exceptions – it’s not part of the JVM at all. So once you’ve got past the compiler, you’re home free.

    I’d strongly advise you to avoid doing this though.

    In many cases there’s a “real” check when you’re using generics because something uses the desired type – but that’s not always the case. For example:

    List<String> strings = new ArrayList<String>();
    List raw = strings;
    raw.add(new Object()); // Haha! I've put a non-String in a List<String>!
    Object x = strings.get(0); // This doesn't need a cast, so no exception...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this piece of code: class Test { public static void main (String[]
Hello I hope someone can explain this problem. This is the code: class Memory{
I am newbie to jQuery, can someone explain what this code does: $(#currency form).submit(function(e)
Can someone explain to me what the bolded portions of this code are doing?
This code is not compilable. I can't find why in standard. Can someone explain?
Can someone write some sample code to explain this concept? I know what a
First of all, here's the code: public class Test{ public static void main(String args[]){
Can someone explain me why this code compiles and runs fine, despite the fact
Can someone explain to me why this code prints Base , Derived but if
Can someone explain this simple, yet deceiving, anomaly? There are two models, where B

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.