In java if we have to execute only one statement after if
or for
the brackets are not necessary. We can write:
if(condition)
executeSingleStatement();
or
for(init;condition;incr)
executeSingleStatement();
But in the case of catch block why we can not omit the brackets? Why this is not possible?
catch(Exception e)
e.printStackTrace();
Because in most of the case we I have only one statement in catch block which is either e.printStackTrace()
while testing or logging statement.
The try statement allows you to define a block of code to be tested for errors while it is being executed. The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.
Core Java bootcamp program with Hands on practice It will generate an exception that is caught by the catch block. The catch block catches and handles the exception. If the catch block is empty then we will have no idea what went wrong within our code.
Java catch block is used to handle the Exception by declaring the type of exception within the parameter. The declared exception must be the parent class exception ( i.e., Exception) or the generated exception type. However, the good approach is to declare the generated type of exception.
Is it necessary that each try block must be followed by a catch block? It is not necessary that each try block must be followed by a catch block. It should be followed by either a catch block or a finally block. And whatever exceptions are likely to be thrown should be declared in the throws clause of the method.
Find a compiler construction textbook and look-up the dangling-else ambiguity.
Given that in Java, and most other languages with horrible syntax, spacing lies. How do you interpret:
try
try
stuff();
catch (FooException exc)
handle(exc);
catch (BarException exc)
handle(exc);
catch (BazException exc)
handle(exc);
Is it:
try {
try {
stuff();
} catch (FooException exc) {
handle(exc);
} catch (BarException exc) {
handle(exc);
}
} catch (BazException exc) {
handle(exc);
}
Or:
try {
try {
stuff();
} catch (FooException exc) {
handle(exc);
}
} catch (BarException exc) {
handle(exc);
} catch (BazException exc) {
handle(exc);
}
The dangling-else ambiguity is resolved by associating the else
with the inner-most if
. Do we want to add a more complicated complication to handle this poor style? No.
Edit: There's a comment that the example does not cover catch
. It would be a proper weird decision to require braces on try
but not catch
/finally
. But anyway, for completeness, consider the following code.
try {
stuff();
} catch (FooException foo)
try {
handle(foo);
} catch (BarException bar)
handle(bar);
catch (BazException baz)
handle(baz);
finally
release();
Is the catch
of BazException
and finally
associated with the inner or outer try
? Again the language design committee could have added a ton of grammar to disambiguate, but again explicit style wins. My job at Sun/Oracle would have been a little easier if the language had been simplified to mandate explicit braces everywhere.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With