try-catch

C# Catch Exception

主宰稳场 提交于 2019-12-23 11:47:55
问题 Which exception would I use in a try/catch to find out when the user has inputted data in the wrong format? Example: try { string s = textBox1.Text; // User inputs an int // Input error MessageBox.Show(s); } catch(what exception) { MessageBox.Show("Input in wrong format"); } Thanks 回答1: Don't do this. It's a misuse of exception handling. What you are attempting to do is considered coding by exception , which is an anti-pattern. An exception is exactly what it sounds like, an exception to the

Is there a static way to throw exception in php

强颜欢笑 提交于 2019-12-23 08:50:23
问题 Is there a "static" way of throwing an exception in php? I need to throw an exception when a mysql query fails. I tried this: $re=@mysql_query( $query ) or throw new Exception(' Query Failed '); but it's not working. And I'm using a function based on the throwException() function from this comment at PHP: Exceptions manual, but I would like to know if there is a static way for doing this without making a class. 回答1: You won't be able to directly do or throw new Exception(); because throw is a

Use of unassigned local variable on finally block

雨燕双飞 提交于 2019-12-23 08:50:13
问题 When could i in this example be unassigned? int i; try { i = 2; } catch { i = 3; } finally { string a = i.ToString(); } 回答1: You could get a ThreadAbortException before i=2 runs, for example. Anyway, the C# compiler is not exceptionally smart, so it's easy to fool with contrived examples like the above one. It doesn't have to recognize every situation, and if it's not sure it is assigned, even if you are sure, it will complain. EDIT: I was a bit quick on my first assumption. So to refine it,

Python — efficiency of caught exceptions [duplicate]

℡╲_俬逩灬. 提交于 2019-12-23 07:28:43
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Python FAQ: “How fast are exceptions?” I remember reading that Python implements a "Better to seek forgiveness than to ask permission" philosophy with regards to exceptions. According to the author, this meant Python code should use a lot of try - except clauses, rather than trying to determine ahead of time if you were about to do something that would cause an exception. I just wrote some try - except clauses

Can't get throws to work with function with completion handler

南楼画角 提交于 2019-12-23 07:22:04
问题 I'm trying to add a throws to my existing function with a completion handler but I keep getting a warning saying no calls throwing functions occur within try expression . In the section where I throw the errors, I get an error saying invalid conversion from throwing function of type '() throwing -> Void' to non-throwing function type. enum LoginError: ErrorType { case Invalid_Credentials case Unable_To_Access_Login case User_Not_Found } @IBAction func loginPressed(sender: AnyObject) { do{ try

Curious C# using statement expansion

笑着哭i 提交于 2019-12-23 06:47:13
问题 I've run ildasm to find that this: using(Simple simp = new Simple()) { Console.WriteLine("here"); } generates IL code that is equivalent to this: Simple simp = new Simple(); try { Console.WriteLine("here"); } finally { if(simp != null) { simp.Dispose(); } } and the question is why the hell does it check null in the finally? The finally block will only be executed if the try block is executed, and the try block will only be executed if the Simple constructor succeeds (I.e. does not throw an

Spring Data JPA + Hibernate : catch block inside a Transactional method is never reached

走远了吗. 提交于 2019-12-23 05:57:07
问题 In our Spring Data JPA + Hibernate application, there are various methods having multiple JPA operations inside the same transaction - below is the code/configuration on one of the spring service methods. @Transactional(rollbackFor=MyAppException.class) public void updateCustomerprofile(String customerKey) throws MyAppException{ try{ CustomerId customerIdObj = customerIdRepository.findOne(customerKey); customerIdObj.setCreatedUser("<loggedin-user>"); // more logic here customerIdObj =

how to manage nested transaction with try catch

[亡魂溺海] 提交于 2019-12-23 05:23:04
问题 --Drop Table Tab1 Begin Transaction TR1; Save Transaction TR1; Create Table Tab1(f1 decimal(10,0)); Begin Transaction TR2 Save Transaction TR2 insert into Tab1 values(1); Begin Transaction TR3; Save Transaction TR3; insert into Tab1 values(2); Begin Try insert into Tab1 values('OK'); Commit Transaction TR3; END TRY BEGIN Catch print 'catch' RollBack Transaction TR3; End Catch insert into Tab1 values(3); Commit Transaction TR2 insert into Tab1 values(4); Commit Transaction TR1; --Commit

Runtime exception not terminating the programm [closed]

馋奶兔 提交于 2019-12-23 05:17:40
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . As per my little-bit java knowledge Program supposed to be terminated after it throws runtime exception. But in my application after throwing runtime exception it is not terminating ,and as i am executing it on linux i have to use ctrl+c to terminate it, Otherwise it just do not terminate. I am creating jar on

tryCatch function works on most non-existent URLs, but it does not work in (at least) one case

本小妞迷上赌 提交于 2019-12-23 04:53:30
问题 Dear Stackoverflow users, I am using R to scrape profiles of a few psycotherapists from Psychology Today; this is done for exercising and learning more about web scraping. I am new to R and I I have to go through this intense training that will help me with a future projects. It implies that I might not know precisely what I am doing at the moment (e.g. I might not interpret well either the script or the error messages from R), but I have to get it done. Therefore, I beg your pardon for