answersLogoWhite

0

The finally clause is used to provide the capability to execute code no matter whether or not an exception is thrown or caught. It is usually used in places where we are connecting to a database so that, we can close the connection or perform any cleanup even if the query execution in the try block caused an exception

User Avatar

Wiki User

13y ago

What else can I help you with?

Continue Learning about Engineering

Which exception is raised by a CASE statement when no match is found and the ELSE clause is omitted from the statement?

case_not_found


How does a try statement determine which catch clause should be used to handle an exception?

When an exception is thrown within the body of a try statement, the catch clauses of the try statement are examined in the order in which they appear. The first catch clause that is capable of handling the exception that was thrown, is executed. The remaining catch clauses are ignored


What does default mean in switch statement?

Default clause in switch statement used to indicate that the desired option is not available with the switch case statement. it is similar to else statement of if statement which is used when the condition does not satisfy.


What is finally statement in java?

Although try and catch provide a great way to trap and handle exceptions, we are left with the problem of how to clean up if an exception occurs. Because execution transfers out of the try block as soon as an exception is thrown, we can't put our cleanup code at the bottom of the try block and expect it to be executed if an exception occurs. Exception handlers are a poor place to clean up after the code in the try block because each handler then requires its own copy of the cleanup code. If, for example, you opened a database connection somewhere in the guarded region, each exception handler would have to release the connection. That would make it too easy to forget to do cleanup, and also lead to a lot of redundant code. If you forget to close the connection in cases where an exception occurs, you will be left with orphan connections which can eventually crash your database. To address this problem, Java offers the finally block. A finally block encloses code that is always executed at some point after the try block, whether an exception was thrown or not. Even if there is a return statement in the try block, the finally block executes right after the return statement is encountered, and before the return executes! This is the right place to close your files, release your db connections, and perform any other cleanup your code requires. If the try block executes with no exceptions, the finally block is executed immediately after the try block completes. If there was an exception thrown, the finally block executes immediately after the proper catch block completes. Let's look at another pseudocode example: 1: try { 2: // This is the first line of the "guarded region". 3: } 4: catch(DatabaseDownException) { 5: // Put code here that handles this exception 6: } 7: catch(SomeOtherException) { 8: // Put code here that handles this exception 9: } 10: finally { 11: // Put code here to release any resource we 12: // allocated in the try clause. 13: } 14: 15: // More code here As before, execution starts at the first line of the try block, line 2. If there are no exceptions thrown in the try block, execution transfers to line 11, the first line of the finally block. On the other hand, if a SomeOtherException is thrown while the code in the try block is executing, execution transfers to the first line of that exception handler, line 8 in the catch clause. After all the code in the catch clause is executed, the program moves to line 11, the first line of the finally clause. To summarize - THE FINALLY BLOCK WILL EXECUTE ALWAYS. There is actually a catch here about the finally block executing always, but I will leave you to ponder over it for sometime. We will look at it a little later.


What are IF statement functions?

A statement and a function are two separate things. An if statement is a selection statement and has the following forms in C: if (expression) { statement; } if (expression) { statement; } else { statement; } In the first form, the statement executes only when the expression evaluates true. In the second form, the first statement executes when the expression evaluates true, otherwise the second statement executes. The second statement may be another if statement (a nested if): if (expression) { statement; } else if (expression) { statement; } else { statement; } Here, the second expression is only evaluated when the first expression evaluates false. If both expressions evaluate false, the final statement executes. Note that the final else clause is optional within nested if statements. Nested ifs can often be thinly-disguised switch statements: if (x==0) { f(x); } else if (x==1) { g(x); } else if (x==2) { h(x); } else { i(x); } If statements of this type are best implemented using a switch statement: switch (x) { case 0: f(x); break; case 1: g(x); break; case 2: h(x); break; default: i(x); } As well as being easier to read (and maintain), execution is more efficient as the control expression (x) need only be evaluated once and execution will immediately pass to the appropriate case label (much like a goto statement). With a nested if statement, each expression has to be evaluated in turn until one of them evaluates true, or execution passes to the else clause. Switch statements are also more flexible in that the default clause need not be the final clause and execution automatically "falls through" to the next case label until a break or return statement is encountered.

Related Questions

What is a grandfather clause and What is its purpose in literacy tests?

What is a grandfather clause, and what was its purpose


What is a grandfather clause and what was it purpose in literacy tests?

What is a grandfather clause, and what was its purpose


A conditional statement consist of a?

"if" clause and a "then" clause. The "if" clause states a condition that must be true in order for the statement to be true, and the "then" clause states the result or outcome if the condition is met.


What clause of the select statement names the table that contains the data to be retrieved?

The FROM clause names the table that contains the data to be retrieved in a SELECT statement.


Which exception is raised by a CASE statement when no match is found and the ELSE clause is omitted from the statement?

case_not_found


What is an inverse statement of if a triangle is an equilateral triangle?

"if a triangle is an equilateral triangle" is a conditional clause, it is not a statement. There cannot be an inverse statement.


Is as you an adverb clause or an adjective clause?

The term 'as you' is not a clause without a verb.But a clause introduced by 'as' is an adverb clause adding how, when, why to the statement made.As you said, it was more expensive than expected.We put the steaks on the fire as you arrived.


Which statement summarizes the enslaved persons clause?

North side


Elastic clause in a sentence?

Elastic clause is a statement in the U.S. Constitution granting Congress the power to pass all laws.


If you took an if then statement inserted a not in each clause and reversed the clauses the new statement would also be true?

true


An introductory adverb clause is usually followed by a comma an adverb clause within a sentence usually does not require punctuation?

This statement is true.


True or false if you took a true “if-then” statement and inserted a not in each clause, the new statement would also be true?

False