try-catch

'try with resource' feature for File class

谁都会走 提交于 2019-12-12 11:08:00
问题 I have one scenario where I am trying to implement with the Java 7 'try with resource' feature. My finally block contains an object of BufferedWriter and File , which I want to close using 'try with resource' feature, instead of closing it by calling close method explicitly. But I checked on net and saw that the File class does not implement the AutoCloseable interface, but BufferedWriter does. So how can I manage this scenario to implement 'try with resource' feature? 回答1: try

Javascript Try/Catch

最后都变了- 提交于 2019-12-12 10:52:02
问题 I've got a function that runs a user generated Regex. However, if the user enters a regex that won't run then it stops and falls over. I've tried wrapping the line in a Try/Catch block but alas nothing happens. If it helps, I'm running jQuery but the code below does not have it as I'm guessing that it's a little more fundamental than that. Edit: Yes, I know that I am not escaping the "[", that's intentional and the point of the question. I'm accepting user input and I want to find a way to

Javascript use strict error not catching

天大地大妈咪最大 提交于 2019-12-12 09:53:00
问题 I am creating a backbone.js app that uses require.js for AMD. In order to check for use strict support in the browser, I have included the following code. However, when the code is run, the error thrown by var o = {p:1, P:2} is not caught as I expect it to be, and instead kills the entire page. Chrome console prints this error: Uncaught SyntaxError: Duplicate data property in object literal not allowed in strict mode require([ 'jquery', 'underscore', 'backbone', 'src/app' ], function(jQuery,

Why won't Scala optimize tail call with try/catch?

我怕爱的太早我们不能终老 提交于 2019-12-12 09:30:18
问题 In a recent StackOverflow answer, I gave the following recursive code: def retry[T](n: Int)(fn: => T): T = { try { fn } catch { case e if n > 1 => retry(n - 1)(fn) } } If I add the @tailrec annotation, I get: Could not optimize @tailrec annotated method retry: it contains a recursive call not in tail position. I was able to hack a tail-recursive alternative, but I still wonder why this didn't optimize. Why not? 回答1: To be tail-recursion optimized, this has to be transformed into something

A try-catch method in while loop?

我的梦境 提交于 2019-12-12 09:22:38
问题 I have this code, and I want to put the try-catch inside a while loop. The logic would be, "while there is an input error, the program would keep on asking for a correct input". How will I do that? Thanks in advance. public class Random1 { public static void main(String[] args) { int g; Scanner input = new Scanner(System.in); Random r = new Random(); int a = r.nextInt(10) + 1; try { System.out.print("Enter your guess: "); g = input.nextInt(); if (g == a) { System.out.println("**************")

How to ignore “Access to the path is denied” / UnauthorizedAccess Exception in C#?

雨燕双飞 提交于 2019-12-12 09:15:27
问题 How to bypass/ignore "Access to the path is denied"/UnauthorizedAccess exception and continue to collecting filenames in this method; public static string[] GetFilesAndFoldersCMethod(string path) { string[] filenames = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Select(Path.GetFullPath).ToArray(); return filenames; } //Calling... ... foreach (var s in GetFilesAndFoldersCMethod(@"C:/")) { Console.WriteLine(s); } My application stops on the firstline of

What is Error in value[[3L]](cond) in R?

本秂侑毒 提交于 2019-12-12 08:28:50
问题 I have a code that has an error because of not enough memory. Actually I do a linear model (lm) on a big data. The problem is not because it gives me the error, that I want to log, but because it contains value[[3L]](cond) . My error looks like this: Error in value[[3L]](cond): While training model Error: cannot allocate vector of size 6.4 Gb The code that logs it look like this (using logging lib): tryCatch({ # some code tryCatch({ # some other code }, warning = function(war){ logwarn(war,

Better way to show error messages in async methods

岁酱吖の 提交于 2019-12-12 08:23:42
问题 The fact that we can't use the await keyword in catch blocks makes it quite awkward to show error messages from async methods in WinRT, since the MessageDialog API is asynchronous. Ideally I would like be able to write this: private async Task DoSomethingAsync() { try { // Some code that can throw an exception ... } catch (Exception ex) { var dialog = new MessageDialog("Something went wrong!"); await dialog.ShowAsync(); } } But instead I have to write it like this: private async Task

Why Kotlin receives such an UndeclaredThrowableException rather than a ParseException?

我的未来我决定 提交于 2019-12-12 08:20:41
问题 I have an extension method that converts string to Date in Kotlin. fun String.convertToDate() : Date { var pattern: String = "dd-mm-yyyy" val dateFormatter = SimpleDateFormat(pattern) return dateFormatter.parse(this) // parse method throw ParseException } And this is the code where I am trying to catch possible exception. try { "22---2017".convertToDate() } catch (ex: ParseException) { // ParseException supposed to be caught in this block logger.error("Parse exception occur") } catch (ex:

Best way to check the type of a variable

大城市里の小女人 提交于 2019-12-12 05:09:52
问题 Yesterday I was doing some testing to identify the type of an element from a list. types={"float":float, "int":int, "str":str} try: sql_type = next (k for k,v in types.iteritems() if isinstance (uniqLst[0],v)) except TypeError as Typeerr: print "Type not right: " + str(Typeerr) Well, of course the element turns out to always be a string as the data the list holds derives from a text file. I was wondering what might be a good way to check the true nature of the element. Should you really go