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

What is BNF for switch case?

BNF, or Backus-Naur Form, is a notation used to express the grammar of programming languages. A switch-case structure can be represented in BNF as follows: <switch-statement> ::= "switch" "(" <expression> ")" "{" <case-clause>* <default-clause>? "}" <case-clause> ::= "case" <constant> ":" <statement>* <default-clause> ::= "default" ":" <statement>* This defines a switch statement consisting of an expression, multiple case clauses, and an optional default clause.


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.

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.


What is BNF for switch case?

BNF, or Backus-Naur Form, is a notation used to express the grammar of programming languages. A switch-case structure can be represented in BNF as follows: <switch-statement> ::= "switch" "(" <expression> ")" "{" <case-clause>* <default-clause>? "}" <case-clause> ::= "case" <constant> ":" <statement>* <default-clause> ::= "default" ":" <statement>* This defines a switch statement consisting of an expression, multiple case clauses, and an optional default clause.


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.


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


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.


What are the two required clauses for a SELECT statement?

The two required clauses for a SELECT statement are the SELECT clause and the FROM clause. The SELECT clause specifies the columns or expressions to retrieve, while the FROM clause indicates the table or tables from which to select the data. These clauses are essential for forming a valid SQL query to extract information from a database.


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