try-catch

How do I close a file after catching an IOException in java?

这一生的挚爱 提交于 2020-01-21 04:41:05
问题 All, I am trying to ensure that a file I have open with BufferedReader is closed when I catch an IOException, but it appears as if my BufferedReader object is out of scope in the catch block. public static ArrayList readFiletoArrayList(String fileName, ArrayList fileArrayList) { fileArrayList.removeAll(fileArrayList); try { //open the file for reading BufferedReader fileIn = new BufferedReader(new FileReader(fileName)); // add line by line to array list, until end of file is reached // when

PowerShell try/catch/finally

こ雲淡風輕ζ 提交于 2020-01-20 13:23:05
问题 I recently wrote a PowerShell script that works great - however, I'd like to now upgrade the script and add some error checking / handling - but I've been stumped at the first hurdle it seems. Why won't the following code work? try { Remove-Item "C:\somenonexistentfolder\file.txt" -ErrorAction Stop } catch [System.Management.Automation.ItemNotFoundException] { "item not found" } catch { "any other undefined errors" $error[0] } finally { "Finished" } The error is caught in the second catch

PowerShell try/catch/finally

若如初见. 提交于 2020-01-20 13:22:28
问题 I recently wrote a PowerShell script that works great - however, I'd like to now upgrade the script and add some error checking / handling - but I've been stumped at the first hurdle it seems. Why won't the following code work? try { Remove-Item "C:\somenonexistentfolder\file.txt" -ErrorAction Stop } catch [System.Management.Automation.ItemNotFoundException] { "item not found" } catch { "any other undefined errors" $error[0] } finally { "Finished" } The error is caught in the second catch

PowerShell try/catch/finally

被刻印的时光 ゝ 提交于 2020-01-20 13:21:11
问题 I recently wrote a PowerShell script that works great - however, I'd like to now upgrade the script and add some error checking / handling - but I've been stumped at the first hurdle it seems. Why won't the following code work? try { Remove-Item "C:\somenonexistentfolder\file.txt" -ErrorAction Stop } catch [System.Management.Automation.ItemNotFoundException] { "item not found" } catch { "any other undefined errors" $error[0] } finally { "Finished" } The error is caught in the second catch

TryCatch jump to the catch

感情迁移 提交于 2020-01-17 19:23:34
问题 How can i jump to the catch block in a try catch : try{ if(bla!=) throws Exception(); }catch(exception){ //do } doesn't work ? 回答1: bla!= should be followed by a condition, throws should be throw , Exception() should be new Exception() , exception should be Exception e try { if (bla != null) // or anything else throw new Exception("Exception thrown"); } catch (Exception e) { System.out.println("Exception caught!"); e.printStackTrace(); } 回答2: Look the Flow of a try-catch-finally Block 来源:

Java skip try catch for throwable fuction

梦想的初衷 提交于 2020-01-17 05:38:14
问题 I wonder if there is a way in Java to 'skip' the try-catch method for a throwable function that I know will not throw an exception. I have this piece of code: DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm"); Date date = format.parse(dateString); // <-- Compiler error here Log.i(PF.TAG, date.toString()); I get compiler error saying an exception is not handled Error:(97, 30) error: unreported exception ParseException; must be caught or declared to be thrown I can get rid of this

Protractor TRY/Catch issues

若如初见. 提交于 2020-01-16 03:56:18
问题 I was using Protractor and Cucumber and noticed that there were instances where I wanted to capture a NoSuchElementError: No element found using locator: By.cssSelector("someCssLocatorHere") . However, using the traditional try/catch block wouldn't work unless I called both the callback and errBack of the .then() function and throw the error so that I can catch it later like following: try{ somePromise.then(function(){ //callback function if promise gets resolved successfully }, function(e){

Refactor long try-except chain

一世执手 提交于 2020-01-16 02:22:33
问题 I have a feeling that this sequence might be written shorter: dim = Dimension.objects.get(pk=rows['pk']) try: dim.name = rows['name'] except KeyError: pass try: dim.external_flg = rows['external_flg'] except: pass try: dim.ext_owner = rows['ext_owner'] except KeyError: pass try: dim.ext_table_name = rows['ext_table_name'] except KeyError: pass try: dim.ext_start_date_column_name = rows['ext_start_date_column_name'] except KeyError: pass try: dim.ext_end_date_column_name = rows['ext_end_date

Exhaustive catch blocks without empty or wildcard in Swift 3.1

限于喜欢 提交于 2020-01-15 11:54:10
问题 Catch clauses in Swift must be exhaustive. Does it mean I always need to use a wildcard or empty catch clauses whenever I want to avoid error propagation? Example: enum Oops: Error { case oh, ouch, meh } func troublemaker() { do { throw Oops.meh } catch Oops.oh {} catch Oops.ouch {} catch Oops.meh {} // Error: Error is not handled because the enclosing catch is not exhaustive } Of course, it is fixed if I add throws to the function. Same goes for adding either catch {} or catch _ {} . But is

Application.Current.Dispatcher.BeginInvoke - Where to place Try & catch?

非 Y 不嫁゛ 提交于 2020-01-15 11:26:10
问题 if I have a dispatcher invoking in background like this: Application.Current.Dispatcher.BeginInvoke(new Action(() => MethodToCall()), DispatcherPriority.Background); Should I have wrap the code above inside a Try & catch or place the try & catch inside the MethodToCall() method? Many Thanks, 回答1: If you really have a case for catching a specific exception then the try { } catch should be placed inside MethodToCall . 回答2: Hi BeginInvoke will Execute your Method in anoster Stack. So a try-catch