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
-1
votes
1 answer

Why isn't this while statement not working in the catch block

I have a simple a HTML document with a script tag, I'm messing around with a try catch and can't get the while statement in the catch block to work. The try and catch simply runs as if the while block isn't there. try { let age =…
Brandon
  • 1,099
  • 1
  • 9
  • 14
-1
votes
5 answers

Flow control in Exception Handling

I am fairly new to Java and unable to understand the flow of control in try-catch-finally blocks. Whenever a exception is caught in a catch block, the code after the catch block is also executed, whether I place it in a finally block or not. Then…
Gaurav Bipul
  • 93
  • 1
  • 1
  • 4
-1
votes
1 answer

I'm getting irregular output for this program? Why?

Finally example public class FinallyExample { public static void main(String[] args) { new FinallyExample().dothework(); } public void dothework() { Object o=null; for(int i=0;i<=4;i++) …
-1
votes
3 answers

How to test statements individually rather than a block of code using Try-Catch?

The code below will fail because Bind() is called on a socket that has not been "prepared", even though there is code to prepare the socket. The code that prepares the socket is out of scope (another Try block). // prepare socket …
paperduck
  • 1,175
  • 1
  • 16
  • 26
-2
votes
4 answers

try-catch-finally order of execution

I'm having a problem understanding the order of execution for the try-catch-finally. All the example I've seen (like in:http://stackoverflow.com/questions/4191027/order-of-execution-of-try-catch-and-finally-block) have a very simple "catch" part…
Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94
-2
votes
0 answers

Exception handling control flow is different in Java vs Javascript?

All psuedocode: Java try { somethingThatOftenThrowsException(); }catch (Exception e){ System.out.println("lets handle this exception: " + e); } finally { System.out.println("Finally block is executed either…
-2
votes
1 answer

Java: try-catch-resources with return and throw new statements

This is my cose: private ResultSetType makeRequest() { try (CloseableHttpResponse response = this.sendRequest(request)) { String responseBody = IOUtils.toString( response.getEntity().getContent(), …
Jordi
  • 20,868
  • 39
  • 149
  • 333
-2
votes
1 answer

How to put Custom Exceptions into Try-Catch-Finally but with If condition at the same time in winforms?

I made three custom exceptions. For the product name, the quantity and the price but when I try to include it in try, it wont show up. I just need the exception to work then make it show up in the Console or Message Box. This is what it was…
Ciel
  • 25
  • 6
-2
votes
1 answer

What is the purpose of try with resource block in java?

What is the purpose of try with resource block in java. I understands that it takes an AutoCloseable object reference like Inputstream, which it case close . I want to know what the advantage of this construct. The same we are able to achieve by…
Abhishek Saxena
  • 292
  • 3
  • 15
-2
votes
2 answers

Trapping and throwing errors when using ExecuteNonQueryAsync which returns Task

When I do the following, I get a warning that not all code paths return a value if the catch block doesn't return an int; when that catch block returns an int, the capturedException test for null becomes unreachable unless it is placed inside a…
Tim
  • 8,669
  • 31
  • 105
  • 183
-2
votes
2 answers

Why finally-block is executed after a try-block containing return statement

I'm wondering why the function foo is returning 3 instead 1. Please explain. def foo(): try: return 1 except: return 2 finally: return 3
nik_kgp
  • 1,112
  • 1
  • 9
  • 17
-3
votes
1 answer

Closing a null sql connection

I have a question about closing an opened connection to a database in C#. Let's say we abandon the "using" method and use a try/catch/finally block to open and close the connection. try { connection = new SqlConnection(); …
-3
votes
1 answer

Why no exception when finally has return clause

The code snap is like below: public static void main(String[] args) { System.out.println(echo("jjj")); } public static String echo(String str) { try { int a = 1/0; } catch (Exception e) { throw e; } finally { …
xuanzhui
  • 1,300
  • 4
  • 12
  • 30
-3
votes
2 answers

Questions on try-catch

I am trying to understand try-catch block. Mostly, I got the point but there are two points, which I did not understand and do not know with which terms should I search them. try{ operation1; operation2; operation3; …
drJava
  • 697
  • 2
  • 9
  • 24
-3
votes
3 answers

Python exception - need finally, don't need except

I have some code which represents a test case within a proprietary testing framework that looks something like this: def test_alarm(self): self.setup_some_test_data() try: self.send_alarm_message() except: print…
Component 10
  • 10,247
  • 7
  • 47
  • 64
1 2 3
28
29