try-catch

How to better write multiple exceptions with redundant code in Python?

谁说我不能喝 提交于 2019-12-11 02:24:39
问题 How can I better write the following snippet in Python: try: statement-1 except Exception1: codeblock-1 codeblock-2 except Exception2: codeblock-2 Just to be clear, I want to execute two codeblocks when the first exception occurs, while only the latter of these two codeblocks when the second exception occurs. 回答1: You have two options, as I see it; either: Extract codeblock-2 into a function and just call it (you repeat only one line this way); or Catch both exceptions in the same except ,

Try/Catch not handling exceptions from Selenium2 FindElement

强颜欢笑 提交于 2019-12-11 02:13:50
问题 I'm playing around with wrapping some Selenium2 API calls in some helper methods, but expected exceptions aren't being handled even though I am catching them. Here's the code: public static bool IsElementPresent(this IWebDriver driver, By by) { var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 5)); wait.IgnoreExceptionTypes(new Type[] { typeof(WebDriverException) }); try { wait.Until(drvr => drvr.FindElement(by)); return true; } catch (NoSuchElementException) { return false; } catch

try/catch throws error

南笙酒味 提交于 2019-12-11 01:53:20
问题 With the help from good samaritans from stackoverflow, I have come till the following code to catch exceptions when the input from user is not an integer: signed int num; while(true) { cin >> num; try{ if(cin.fail()){ throw "error"; } if(num>0){ cout<<"number greater than 0"<<endl; } } catch( char* error){ cout<<error<<endl; break; } } Now assume the program is called: checkint. If I call the program by redirecting the input from a text file, say: input.txt, which has the following contents:

Why do we need a function try block?

南楼画角 提交于 2019-12-11 01:16:59
问题 This is a follow-up question of the post. Please see the end of this question for a definition of "function try block". Question: If a function try block does not "handle" the exception raised in the constructor, why do we need them after all? Could you give an example taking advantage of function try block? Considering the following code. #include <iostream> #include <new> #include <exception> using namespace std; struct X { int *p; X() try : p(new int[10000000000]) {} catch (bad_alloc &e) {

Eclipse does not detect missing try/catch anymore?

≡放荡痞女 提交于 2019-12-11 01:08:55
问题 In this code String a = "notANumber"; Integer b = Integer.parseInt(a); A try/catch is necessary since parseInt throws an NumberFormatException exception. In my previous version of Eclipse, I used to got a warning telling that a try/catch was necessary but I can't figured out how to enable it on my current version of Eclipse which is Eclipse Java EE IDE for Web Developers. Version: Kepler Service Release 1 回答1: A NumberFormatException is a RuntimeException . You don't have to put try / catch

How to declare a global variable in this API?

让人想犯罪 __ 提交于 2019-12-11 01:08:48
问题 My application has a production or development setting that I can toggle. When setting up the server, I set up this flag in Applications > Mamp > Conf > Apache > httpd.conf . Its purpose is to give my local API directory a server alias. It also defines the document root etc. Listen 44447 <VirtualHost *:44447> DocumentRoot "/Users/user/Desktop/PushChatServer/api" ServerName 192.168.1.5:44447 ServerAlias pushchat.local CustomLog "/Users/user/Desktop/PushChatServer/log/apache_access.log"

How to use Exception Handling in Selenium Webdriver?

喜你入骨 提交于 2019-12-11 00:53:26
问题 The Scenario is as follows: When the code driver.get(url); runs, if the URL is not able to access (if server is not reachable) then it should throw an exception that server is not reachable and the code should stop executing the next line driver.findElement(By.id("loginUsername")).sendKeys(username); The following code I'm running in eclipse as follows: import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Properties; import javax

Wrapping Functions in Try-Catch Block in Javascript

时光毁灭记忆、已成空白 提交于 2019-12-10 23:38:30
问题 Is it possible to wrap functions inside a try-catch block? It appears to not work for the first function, but would it work for the prototype function declared that way? Example: try { function MyFunction1() { //function code here } MyFunction1.prototype.getValue = function() { //more code here } } catch (e) { //error handling here } 回答1: No, it's not possible to catch an exception in that way. A try/catch block around a function definition does not catch exceptions thrown from that function.

Try Catch - not catching

岁酱吖の 提交于 2019-12-10 22:19:40
问题 Here's my problem. I have a middleware web service used by several web clients. The web service is a wrapper service that makes calls to several vendors' web services. The call to the vendor's web service is wrapped in a TryCatch but any exceptions generated aren't getting caught by my web service, they're getting caught in the client apps. Am I missing something? Is this related to this posting? Here's a simplified code snippet: Dim VendorWebservice as New VendorWebservice Dim VendorResponse

Asking user to enter the input again after he gives a wrong value for the Input. InputMismatchException?

点点圈 提交于 2019-12-10 21:31:57
问题 I have created the following class for Inputting a user's age and then displaying appropriate info in the console. On running this program , the console asks "Please Enter your Age : " If the user enters an Integer for eg: 25 , the executed class displays " Your age is : 25" in the console. If the user enters a non-integer number , the console displays: Age should be an Integer Please Enter your Age: But I am not able to enter anything through the keyboard when I place my cursor next to