Questions tagged [try-catch-finally]

A common usage of catch and finally together is to obtain and use resources in a try block, deal with exceptional circumstances in a catch block, and release the resources in the finally block.

435 questions
0
votes
1 answer

Throwing an exception in a catch block with hibernate before close in finally

So, I want some confirmation on this. I will explain in pseudo code. public void myFunc() throws Exception{ Session session = Hibernate.getSession(); try{ //do somthing } catch(Exception ex){ throw ex; } finally{ session.close(); …
NullPointer0x00
  • 1,808
  • 3
  • 18
  • 20
0
votes
0 answers

Spring JdbTemplate code base resource closing strategy

According to my knowledge, finally block is the best place to close any open resources. (Refer: https://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html) But I am surprised when I have looked at Spring v4.3.5 JdcbTemplate class…
MFH
  • 357
  • 3
  • 17
0
votes
1 answer

Using an else-if statement with in a try-catch-finally

I am trying to use a try-catch-finally statement to error check user input and then advance to the next page if there are no error (I know there's easier ways to do this but I'm being required to use a try-catch-finally). In this case: foo is the…
0
votes
1 answer

PHP try catch skipping catch, directly jumping into finally

Ok, i'm very confused. I'm using the Slim framework and I have following Code: 1 $app->post("/user/login", function (Request $request, Response $response) { 2 $resp = null; 3 try { 4 if (!array_key_exists("password",…
Aaronmacaron
  • 309
  • 3
  • 10
0
votes
4 answers

Try/Catch within loop in C#

There is a try/catch/finally block within the for or foreach loop. What will happen if there is a break statement within the try block? Will finally block be executed?
Flow.NK
  • 368
  • 1
  • 8
  • 17
0
votes
1 answer

Hibernate transaction: what is the point of this code?

In my project the following code is being used to manage hibernate transactions: public void someMethod() { Session session = HibernateSessionFactory.getSession(); Transaction tx = session.beginTransaction(); try { …
lukas84
  • 428
  • 3
  • 16
0
votes
0 answers

Can / Should `signalAll` be within the `finally` block?

When using a Lock with a Condition, e.g., ReentrantLock lock = new ReentrantLock(); Condition condition = lock.newCondition(); and locking, i.e., lock.lock(); then while it is not ok to continue, e.g., try { while (!ok_to_continue) { we await…
Erik
  • 4,305
  • 3
  • 36
  • 54
0
votes
1 answer

Why the code after exception raised in try block is not executed? if the exception is not handled ,then the control goes out?

class TestFinallyBlock1{ public static void main(String args[]){ try{ int data=25/0; System.out.println(data); } catch(NullPointerException e){System.out.println(e);} …
0
votes
3 answers

Not able to reach catch block in javascript

I am trying to implement try catch block in javascript, but my question is why am i not getting send to the catch block when some error is thrown in my code, for instance try { var a = 10; var b = 0; …
Lijin Durairaj
  • 4,910
  • 15
  • 52
  • 85
0
votes
2 answers

C# try catch finally Run code only if exception is unhandled

I have the following function in my C# helper library which updates an object in a third-part application (CATIA V5): public void Update(INFITF.AnyObject objectToUpdate) { try { Part.UpdateObject(objectToUpdate); …
Eric
  • 1,392
  • 17
  • 37
0
votes
0 answers

putting heavy code in finally block

I am adding a new code in my application , and this code saves some data in DB. Now, this code is after a try, catch block. If the exception occurs, the catch block returns the exception. So in that case, my code(after the catch block) will not be…
Amanpreet Khurana
  • 549
  • 1
  • 5
  • 17
0
votes
1 answer

Find last object from a list of objects, throw exception if not found

Requirements: find last Bar object from a list of different objects, throw NoSuchElementException if not find Bar findLast(List stuff) throws NoSuchElementException { } My solution: Bar findLast(List stuff) throws NoSuchElementException { …
SHE
  • 23
  • 1
  • 6
0
votes
1 answer

Control flow is not passing over to finally block if exception occurs in try block of try-finally

I have the following c# code: public class Program { static void Main() { int i = 123; string s = "Some string"; object obj = s; try { // Invalid…
Kifayat Ullah
  • 579
  • 1
  • 5
  • 14
0
votes
2 answers

What is the need of finally block when we can close files and all in catch block

What is the need of finally block when we can close files and all in catch block. Whatever thing we are closing or clearing in finally block can it be done in catch block. Please tell me if I am wrong.
pradeep
  • 31
  • 7
0
votes
1 answer

Using 'finally' to call a method by Design

In a try-catch-finally situation what is the proper way of handling finally when it comes to calling a method? The reason why I ask is a Service Implementation class that makes DB calls might have a finally clause in every method. If I am to use the…
wiredniko
  • 2,491
  • 3
  • 19
  • 37