try-catch

Handling exceptions vs. Preventing them from occuring in the first place - C#

﹥>﹥吖頭↗ 提交于 2019-12-11 04:33:21
问题 I have a asp:BoundColumn with DateTime? data (i'll call it column X) in the asp:GridView. I loop through all the rows in the grid, and cast the .Text property to column X to DateTime (i need this to do some checking and change the cell background color if needed). I have 2 options: wrap the cast in try/catch ... handle FormatException (in case column X gets rendered with some junk in it) ... and work only with valid DateTime values. check the column X to make sure it's in the correct format

Catch different Exception types

微笑、不失礼 提交于 2019-12-11 04:04:18
问题 I have a very simple function to check whether an Entity exists in a bundle: public function checkExists($bundle, $val) { try{ $this->em->getRepository($bundle.':'.$val); }catch (MappingException $e){ return false; } return true; } So I have the following cases: Input | Expected | Actual 'AppBundle', 'Company' | true | true 'AppBundle', 'NONEXISTANT' | false | false (MappingException caught) 'NONEXISTANT', 'Company' | false | 500 (ORMException not caught) 'NONEXISTANT', 'NONEXISTANT' | false

Java catching exceptions and subclases

ⅰ亾dé卋堺 提交于 2019-12-11 03:57:37
问题 Hello, In Java if a method like BufferedReader.read() says it can throw an IOException and I try to catch a FileNotFoundException and an IOException in two catch blocks, what catch blocks will be entered if the file doesn't exist? Does it enter only the most specific or both? 回答1: The first coded catch that matches the exception will be entered. Edited to incorporate comment from Azodius For example: try { bufferedReader.read(); } catch (FileNotFoundException e) { // FileNotFoundException

TryCatch ConcurrentModificationException catching `30% of the time

孤街醉人 提交于 2019-12-11 03:33:48
问题 I'm using an iterator to remove a projectile from a list if it goes out of the border of my JPanel. Before using the iterator it would not work, but with the iterator it works so long as I put the method into a try-catch for a ConcurrentModificationException . The code now works, and successfully removes the projectile from the list, but about 30% of the time, the catch hits and causes a stutter in my program. I don't know why it only catches sporadically, but in the limited time my professor

Use tryCatch within R loop

被刻印的时光 ゝ 提交于 2019-12-11 03:29:37
问题 I want to read prices and compute returns for data obtained from Yahoo, passing over symbols for which data could not be read. The code library("TTR") source("util.r") symbols = for (sym in c("CSI","XCSIX","IGI")) { cat("\nreading data for",sym,"\n") tryCatch( { stk = getYahooData(sym, start = 20000101, end = 20200101, freq = "daily") ndays = length(index(stk)) logret = ((log(stk$Close) - lag(log(stk$Close)))) [2:ndays] print(summary(logret))}, error = cat("could not read data for ",sym)) }

Using Unique Key in SQL with or without try/catch as a valid way to verify integrity of data

丶灬走出姿态 提交于 2019-12-11 03:16:26
问题 I have a table with some columns and I've set Unique Key on 3 of those columns to make sure there are no duplicates in the table. Now I was wondering if using try / catch to swallow exceptions throw on duplicates and continue updating inserting next rows in foreach is a good way to go? try { sqlWrite.ExecuteNonQuery(); } catch (SqlException sqlException) { if (!sqlException.ToString().Contains("Violation of UNIQUE KEY constraint")) { MessageBox.Show("Error - " + Environment.NewLine +

java try catch when is the program flow interrupted?

℡╲_俬逩灬. 提交于 2019-12-11 02:45:14
问题 Hello i am not that familiar with Exception Handling in Java so: As the Topic says in a basic try/catch block, when i catch an Exception in the Try block, when is the Program flow interrupted? try{ //some code that raises an Exception }catch(Exception e){ // react to interrupt or continue program flow }finally{ // always done after the catch } //when is this code executed? The finally statement is always executed after a try catch, so what has to be done in the catch part to either interrupt

try/catch vs AppDomain.UnhandledException to log & crash the application

人走茶凉 提交于 2019-12-11 02:37:28
问题 As far as I know, you are supposed to only use try/catch when you will actually handle the exception and not just report&log it and then crash the application. Otherwise, you are better off just checking different scenarios where it makes sense (e.g. if sth==null) or - if your purpose is just to log the exception and crash the application - to use AppDomain.UnhandledException. But is this always the case and why? Suppose the following method, which accepts an array and returns MemoryStream

Should try-catch be avoided for known cases

ぐ巨炮叔叔 提交于 2019-12-11 02:34:53
问题 I have a case which I know will happen but very scarce. For example in every 10 thousand times the code runs, this might happen once. I can check for this case by a simple if but this if will run many times with no use. On the other hand I can place the code in try-catch block and when that special case happens I do what is needed to recover. The question is which one is better? I know that generally speaking try-catch should not be used for known cases because of the overhead issue and also

Java Try With Resources Does Not Work For Assignment?

自古美人都是妖i 提交于 2019-12-11 02:24:45
问题 Alright, so I was just writing a quick class and I tried to use the try with resources instead of the try-catch-finally (hate doing that) method and I keep getting the error "Illegal start of type". I then turned to The Java Tutorials section on it: http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html and it showed that you can assign a new variable in the parenthesis. I'm not sure what is going on. private static final class EncryptedWriter { private final Path