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
vote
2 answers

Try/Catch/Finally, use exception from Catch in Finally?

I've looked at a few other try catch finally questions on here but I'm not sure that this one has been answered. Does it smell bad to do something like: Exception? ex = null; try { //something } catch (Exception e) { ex = e ;} finally { …
Jason
  • 51,583
  • 38
  • 133
  • 185
1
vote
2 answers

Finally block not working in JAVA Stored procedure for Oracle

When I compile the below code, it shows error "cannot find symbol variable out" But if i comment the code in the finally block, I am able to compile successfully. Please advise. public static int writeFile (String p_file_path, String p_data) throws…
0
votes
2 answers

Cannot find place to insert finally block to get rid of the error: Insert Finally to complete TryStatement

I've tried several spots to insert the finally block but no matter what I try it ends up making the code worse. Here is my code, the 4th to last ending curly bracket is the one giving me the error. Any thoughts? package com.tunestore.action; …
Turk
  • 269
  • 2
  • 11
  • 17
0
votes
7 answers

Exception Handling; Try Catch

Here's my code: class FinallyDemo { static void myMethod(int n) throws Exception{ try { switch(n) { case 1: System.out.println("1st case"); return; case…
Kraken
  • 23,393
  • 37
  • 102
  • 162
0
votes
2 answers

using try vs TryCatch in R

My R code is as follows errors = 0 for(i in c(1:100)){ tryCatch(expr = { API Call }, error = {errors=errors+1}, finally = {}) further code } The code is meant to continue execution even if the API call fails and count the number of…
0
votes
0 answers

Is there a way to change a conditional using a Try/Catch/Finally block in Powershell 5.1?

Is there a way to do something similar to what is listed below: Try{ if(<#conditional here#>) } catch[ArgumentOutOfRangeException]{ } Finally{ #Set if statement to false, and run else statement } This is what…
0
votes
3 answers

Why does my counter variable not come back to 0 in a multithreaded scenario?

I'm building a Windows Service base class to manages the polling off a schedule's table of any pending task and the running them. The Windows service is using the System.Timers.Timer to start the schedule's table polling. I'm setting the…
Giuseppe Romagnuolo
  • 3,362
  • 2
  • 30
  • 38
0
votes
1 answer

Using tryCatch append output of user-defined functions to a list and warnings/Error messages to a vector in R

Suppose I have a Null vector and Null List - ot_vec = c() msg_lst = list() Further, suppose I have many user-defined functions. Now I want to use a tryCatch to append the outputs of the user-defined functions in the Null list and the messages from…
0
votes
1 answer

Try/catch block in an infinity loop to print out something, if there is an infinity loop

this is my code (which is an infinity while loop. I should implement a try/catch block here, so that it stops, because it's going to infinity. My professor says we should implement a 'OutOfMemoryError', but I'm not sure how. It still goes to…
0
votes
1 answer

How to tryCatch the same function call multiple times (N times) in R

We have a basic tryCatch that writes a dataframe to Google Sheets, and trys again if the first write fails for any reason: result = tryCatch({ print('TRYING') googlesheets4::sheet_write(data = our_df, ss = our_spreadsheet, sheet =…
Canovice
  • 9,012
  • 22
  • 93
  • 211
0
votes
0 answers

return does not wait for execution of for loop to complete

I have this function const groupList = async (io, socket, userid) => { try { var response = {}; var data = [] ddb.get({ TableName: "TableName", Key: { Username: userid } }).promise() .then(async (user) => { const group =…
user15376850
0
votes
1 answer

finally block running before try block ends db call

I am performing input operation on db and trying to return response. var response = {} try { db.put({},(err, data) => { if(err) { response["message"] = err.message response["code"] = 400 …
user15376850
0
votes
1 answer

Powershell Catch Error: Unable to find a default server with Active Directory Web Services running

Need help to catch the error. Whenever my network connection is unstable my get-ad script will be terminated. $ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path $Date = Get-Date -Format yyyy_MM_dd_THHmm Start-Transcript -Path…
NOBODY519
  • 1
  • 2
0
votes
2 answers

Python everything in try block

I am writing a large batch-type script in Python, and need to do some cleanup in the end, regardless of whether an exception occurred. To do this I'm simply putting the main program in a try block and the cleanup in a finally block. This seems to…
Hypercube
  • 1,181
  • 2
  • 10
  • 16
0
votes
1 answer

scanner.close() Does not work when I use try/finally

import java.util.Scanner; public class userInput { public static void main(String[]args){ try{ Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); int age =…