exception-handling

Raise exception if script fails

让人想犯罪 __ 提交于 2019-12-10 21:03:36
问题 I have a python script, tutorial.py. I want to run this script from a file test_tutorial.py, which is within my python test suite. If tutorial.py executes without any exceptions, I want the test to pass; if any exceptions are raised during execution of tutorial.py, I want the test to fail. Here is how I am writing test_tutorial.py, which does not produce the desired behavior: from os import system test_passes = False try: system("python tutorial.py") test_passes = True except: pass assert

Which exception to raise if a given string does not match some format?

喜欢而已 提交于 2019-12-10 20:52:50
问题 This is a follow up to an older question. Given a ISBN number, e.g. 3-528-03851-5 which exception type should I raise if the passed in string doesn't match the format X-XXX-XXXXX-X? 回答1: Raise a ValueError . It's pretty much the standard way of saying "you've given me a value that doesn't make sense". For example: >>> int("a") Traceback (most recent call last): File "", line 1, in ValueError: invalid literal for int() with base 10: 'a' >>> import shlex; shlex.split("'") Traceback (most recent

Can Java throw a null reference to an exception? [duplicate]

倾然丶 夕夏残阳落幕 提交于 2019-12-10 20:23:59
问题 This question already has answers here : Why can I throw null in Java? [duplicate] (7 answers) Closed 5 years ago . Consider this code: try { .... } catch (MyException e){ /*Can e be null here?*/ } Given that null in Java is a typed null reference, is it ever possible that the catch block above will be entered with e as null ? 回答1: e will never be null . Even if null gets thrown somewhere for some reason, e will just be a NullPointerException . 来源: https://stackoverflow.com/questions/22378147

Signal Handler to stop Timer in C

狂风中的少年 提交于 2019-12-10 20:22:26
问题 I am trying to have a signal handler stop a timer without exiting my program. How should I go about. I want StopTimer to handle the signal to stop the timer #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <sys/time.h> #include <signal.h> #include <unistd.h> #define INTERVAL 2 // number of seconds to go off int main(int argc, char* argv[]) { TimerSet(INTERVAL); while(1) { // do stuff } return 0; } void TimerSet(int interval) { printf("starting timer\n"); struct itimerval

How to catch exceptions with Qt platform independently?

て烟熏妆下的殇ゞ 提交于 2019-12-10 20:04:48
问题 I'm using boost::date_time in my project. When date is not valid it thorws std::out_of_range C++ exception. In Qt's gui application on windows platform it becomes SEH exception, so it doesn't catched with try|catch paradigm and programm dies. How can I catch the exception platform independently? try{ std::string ts("9999-99-99 99:99:99.999"); ptime t(time_from_string(ts)) } catch(...) { // doesn't work on windows } EDITED: If somebody didn't understand, I wrote another example: Qt pro file:

JPA data access object - exception handling and rollback

北战南征 提交于 2019-12-10 19:49:48
问题 I'd like to know what the best way of exception handling in data access objects is, I'm interested in production quality code. As an example public UserDaoImpl implements UserDao{ @PersistenceContext private EntityManager em; void save(User user){ em.persist(user); } User getById(long id){ return em.find(User.class,id); } } Let's say that for example I have a RegisterService somewhere where at some point I'd like to save the user to the database. And that each User needs to have a unique

If .Create() can't instantiate, should it return empty object, null, or throw an exception?

拜拜、爱过 提交于 2019-12-10 19:46:21
问题 I want to be able to instantiate any object in my application with this kind of code: SmartForm smartForm = SmartForm.Create("id = 23"); Customer customer = Customer.Create("id = 222"); I'm now debating what Create() should return if that object does not exist. if Create() returns an empty object , then this is kind of a "null pattern" and I can still pass that object around my application and call methods on it, which makes programming with this model convenient and easy if Create() returns

Correct Way to Throw and Catch Exceptions using async/await

我怕爱的太早我们不能终老 提交于 2019-12-10 19:46:09
问题 All, please take the following code: Task<bool> generateStageAsyncTask = null; generateStageAsyncTask = Task.Factory.StartNew<bool>(() => { return GenerateStage(ref workbook); }, this.token, TaskCreationOptions.LongRunning, TaskScheduler.Default); // Run core asynchroniously. bool bGenerationSuccess = false; try { bGenerationSuccess = await generateStageAsyncTask; } catch (OperationCancelledException e) { // Script cancellation. result.RunSucceeded = true; result.ResultInfo = "Script

How to import a .NET DLL having App.config with a configuration section handler?

怎甘沉沦 提交于 2019-12-10 19:46:03
问题 I am trying to use a DLL from our in-house .net application. The DLL has a App.config file, and has a config section that specifies a configuration handler. I am unable to get my PowerShell script to load this dll. I have boiled the problem down into the simplest form I can. Here is the PowerShel script I am trying: [appdomain]::CurrentDomain.SetData("APP_CONFIG_FILE", "D:\CustomConfig\CustomConfig\CustomConfigTestHarness\bin\Debug\CustomConfigTestHarness.exe.config") Add-Type -Path 'D:

Catching the wrong exception

。_饼干妹妹 提交于 2019-12-10 19:39:32
问题 I am trying to catch a specific exception using MySQL in Java . However, it is running the catch (SQLException ex) instead of the one I want it to. catch (MySQLIntegrityConstraintViolationException ex) { } catch (SQLException ex) { } Getting the following error, I would expect it to run the catch (MySQLIntegrityConstraintViolationException ex) function. 11:12:06 AM DAO.UserDAO createUser SEVERE: null com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry