Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

CAN I THROW EXCEPTION IN BLOCK "try" AMD IMMEDIATELY CATCH IT IN "catch" BLOCK

CAN I THROW EXCEPTION IN BLOCK "try" AMD IMMEDIATELY CATCH IT IN "catch" BLOCK

So, can I do something like this:

    try {
        ...
        throw new RuntimeException();

    }catch (Exception e) {
        e.printStackTrace();
    }

and get RuntimeException() StackTrace ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can do that: it would be legal Java to do so. However, you don’t need to.

Although Exceptions (or, more generally, Throwables) are typically only created in a throw new SomeKindOfException();-type statement, they’re just objects like any other: you can create them without throwing them, assign them to variables, return them from methods etc.

So, you can more easily just do:

new RuntimeException().printStackTrace();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading