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
5
votes
6 answers

python try/finally for flow control

I'm sure this concept has come up before but I can't find a good, simple answer. Is using try/finally a bad way to handle functions with multiple returns? For example I have try: if x: return update(1) else: return…
Falmarri
  • 47,727
  • 41
  • 151
  • 191
5
votes
3 answers

ClassNotFoundException I didn't succeed to catch

I read a lot of pages about the try/catch/finally process in java, but I didn't succeed to catch the ClassNotFoundException in my code. I used a for loop to open and save in rdfxml format every file in a directory I give as the argument. What I want…
Cyril
  • 485
  • 4
  • 15
5
votes
5 answers

try-finally block clarification

When I try to execute the following function in Java: public static int myfunc (int x) { try { return x; } finally { x++; } } public static void main (String args[]) { int y=5,z; z = myfunc(y); …
new_c_user
  • 123
  • 2
  • 12
5
votes
1 answer

Why is catch block optional?

I have the following code public static void nocatch() { try { throw new Exception(); } finally { } } Which gives the error Exception in thread "main" java.lang.Error: Unresolved compilation problem: Unhandled…
codeMan
  • 5,730
  • 3
  • 27
  • 51
5
votes
2 answers

Why does Java not support retrieval of exceptions from try/catch lost when an exception is thrown from finally?

In Java 7, the feature was added to (via getSuppressed()) get exceptions thrown from the implicit finally block of a try-with-resources statement. There still doesn't seem to be a way (that I know of) to do the opposite - when there is an explicit…
Mer
  • 762
  • 1
  • 6
  • 12
5
votes
6 answers

Is catch a method in java?

I have searched for some answers , but could not find & hence I am raising this as a new question. The catch method in the try-catch. Is it a method ? , Its taking an object of type Exception as an argument. But if its a method , Who calls this…
Tito
  • 8,894
  • 12
  • 52
  • 86
5
votes
8 answers

In java, is there a way to ensure that multiple methods get called in a finally block?

So I have a try/finally block. I need to execute a number of methods in the finally block. However, each one of those methods can throw an exception. Is there a way to ensure that all these methods are called (or attempted) without nested finally…
sangfroid
  • 3,733
  • 11
  • 38
  • 42
4
votes
5 answers

Why won't my "finally" run?

I assume I'm missing something really trivial here but for reason it's not obvious to me. I've always assumed that "finally" always executes, regardless of an exception or not. Anyway, this code failed to run and I'm not sure why. It gets to i = i/j…
Diskdrive
  • 18,107
  • 27
  • 101
  • 167
4
votes
1 answer

JavaScript try catch finally block change precedence of errors

Sometimes I have a code in try-catch-finally block when finally branch does some cleanup code which may throw an Error. An error in the finally branch suppresses a possible error in the main branch. I want exactly the opposite: see an error from the…
xmedeko
  • 7,336
  • 6
  • 55
  • 85
4
votes
2 answers

The order of return value with try catch finally

I use this code to test try catch finally: public class My{ public static void main(String[] args) { System.out.println(fun1()); System.out.println(fun2()); } public static int fun1() { int a = 1; try { …
CR7
  • 125
  • 7
4
votes
2 answers

JavaScript Promise.prototype.finally() that receives arguments

Why doesn't Promise.prototype.finally() receive any arguments? The documentation says: A finally callback will not receive any argument, since there's no reliable means of determining if the promise was fulfilled or rejected. This use case is…
McKabue
  • 2,076
  • 1
  • 19
  • 34
4
votes
3 answers

How to use try-finally construction in C#?

We've seen many questions about try-catch-finally and try-finally constructions on this forum. The number of answers increases the number of questions, so I have few too. Here's a link into Microsoft explanation try-finally construction. I've…
4
votes
2 answers

Python try except finally

It looks like I don't quite have the hang of Exception handling yet. I'm at a loss :( The following code sometimes returns this error: File "applications/pingback/modules/plugin_h_pingback.py", line 190, in ping …
hcvst
  • 2,665
  • 2
  • 22
  • 25
4
votes
3 answers

Why is finally useful in java?

I wonder why finally is useful after a try catch test? In any scenario, the code defined after the finally statement will be executed. What is the difference with these two codes? try{ int a = 1 / 0; } catch(ArithmeticException e){ …
M.Ferru
  • 400
  • 1
  • 6
  • 20
4
votes
1 answer

sonarqube 6.3 error could-not-complete-symbolic-execution-reached-limit-of-16000 steps

We have a scan that aborts on the code below, with the exception: org.sonar.java.se.ExplodedGraphWalker$MaximumStepsReachedException: reached limit of 16000 steps for method getServiceProviders#151 in class ServiceProviderService We looked at rules…