What Does a "Do Statement" Without Catch Block Mean

What does a do statement without catch block mean?

It's a new scope of code: thus you can use many do statements if you want to reuse a variable name. Like in the snippet in your question, the variables mars, sz and r exist in both scopes without errors.

A do statement may be labeled, which gives you the ability to get out of that scope:

scopeLabel: do {
for i in 0..<10 {
for j in 0..<20 {
if i == 2, j == 15 {
break scopeLabel
}
else {
print(i,j)
}
}
}
}

For more details, have a look here.

Try block without any catch statements

And where in the code would a StreamException be caught?

The try has a finally but no catch. The finally would execute, and the Exception would propagate to the caller.

Does it make sense to do try-finally without catch?

This is useful if you want the currently executing method to still throw the exception while allowing resources to be cleaned up appropriately. Below is a concrete example of handling the exception from a calling method.

public void yourOtherMethod() {
try {
yourMethod();
} catch (YourException ex) {
// handle exception
}
}

public void yourMethod() throws YourException {
try {
db.store(mydata);
} finally {
db.cleanup();
}
}

Why write Try-With-Resources without Catch or Finally?

As explained above this is a feature in Java 7 and beyond. try with resources allows to skip writing the finally and closes all the resources being used in try-block itself. As stated in Docs

Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.

See this code example

static String readFirstLineFromFile(String path) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
return br.readLine();
}
}

In this example the resource is BufferReader object as the class implements the interface java.lang.AutoCloseable and it will be closed whether the try block executes successfully or not which means that you won't have to write br.close() explicitly.

Another important thing to notice here is that if you are writing the finally block yourself and both your try and finally block throw exception then the exception from try block is supressed.

While on the other hand if you are using try-with-resources statement and exception is thrown by both try block and try-with-resources statement then in this case the exception from try-with-resources statement is suppressed.

As the @Aaron has answered already above I just tried to explain you. Hope it helps.

Source: http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

Java Try Catch Finally blocks without Catch

If any of the code in the try block can throw a checked exception, it has to appear in the throws clause of the method signature. If an unchecked exception is thrown, it's bubbled out of the method.

The finally block is always executed, whether an exception is thrown or not.

Use a 'try-finally' block without a 'catch' block

You would use it to ensure some actions occur after the try content or on an exception, but when you don't wish to consume that exception.

Just to be clear, this doesn't hide exceptions. The finally block is run before the exception is propagated up the call stack.

You would also inadvertently use it when you use the using keyword, because this compiles into a try-finally (not an exact conversion, but for argument's sake it is close enough).

try
{
TrySomeCodeThatMightException();
}
finally
{
CleanupEvenOnFailure();
}

Code running in finally is not guaranteed to run, however the case where it isn't guaranteed is fairly edge - I can't even remember it. All I remember is, if you are in that case, chances are very good that not running the finally isn't your biggest problem :-) so basically don't sweat it.

Update from Tobias: finally will not run if the process is killed.

Update from Paddy: Conditions when finally does not execute in a .net try..finally block

The most prevalent example you may see is disposing of a database connection or external resource even if the code fails:

using (var conn = new SqlConnection("")) // Ignore the fact we likely use ORM ;-)
{
// Do stuff.
}

Compiles into something like:

SqlConnection conn;

try
{
conn = new SqlConnection("");
// Do stuff.
}
finally
{
if (conn != null)
conn.Dispose();
}

What does it mean in .Net: try-catch block without any Exception as parameter for catch?

It means the catch block will catch any exception.

It also means that you can't do anything with the exception object as you don't have a reference to it.

You may use this pattern when you genuinely don't care about anyexception occuring (and don't want to do anything with it), but generally you should avoid this style.

try/finally without catch with return statement?

See JLS 14.17

It can be seen, then, that a return statement always completes abruptly.

The preceding descriptions say "attempts to transfer control" rather than just "transfers control" because if there are any try statements (§14.20) within the method or constructor whose try blocks or catch clauses contain the return statement, then any finally clauses of those try statements will be executed, in order, innermost to outermost, before control is transferred to the invoker of the method or constructor. Abrupt completion of a finally clause can disrupt the transfer of control initiated by a return statement.

Espacially check the phrases attempts to transfer control and the last sentence. The try return attempts to transfer the controll, aftwerwards the finally disrupt the transfer of control initiated by a return statement.

In other words, the try attempts to transfer the controll, but since the execution of the finally block is still open for execution and contains a return statement the attempted transfer of controll in the finally block has a higher preceding. That´s why you see the value 3, which is returned inside the finally block.



Related Topics



Leave a reply



Submit