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 ?
>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();