exception-handling

LINQ to Entites: How should I handle System.InvalidOperationException when checking for existance of an item?

笑着哭i 提交于 2019-12-11 06:32:47
问题 I have a many-to-one relationship that users can edit via checkboxes. PK of Foo is ID, and fid contains the id from the checkbox. I'm checking to see if an element exists with: Foo ent; try { ent = ctx.Foo.First(f => f.ID == fid); } catch (System.InvalidOperationException ioe) { ent = new Foo(); } It seems to me that I should be able to do this without throwing an exception. What would be the best way to do this? 回答1: The InvalidOperationException you get has the message: Sequence contains no

Exception Handling in Qt for Class Hierarchy

限于喜欢 提交于 2019-12-11 06:28:42
问题 I am working on developing a Qt application for windows platform. I am facing problem when using exception handling in class hierarchy. I have Class B object instantiated in a function of Class A. When, due to some reason, an exception is thrown from Class B (and not caught in Class B) it is not being caught in Class A (appropriate try-catch block is present in Class A) and instead the application crashes showing some windows specific error. This type of try-catch mechanism in class hierarchy

Where to catch unhandled exception when editing datagridview

主宰稳场 提交于 2019-12-11 06:24:12
问题 I have a datagridview which is databound to an Entity Framework "table": public ObjectSet<TEntity> tableData { get; private set; } private BindingSource tableBinding; public AuxiliaryTableEditor(ObjectSet<TEntity> something) { InitializeComponent(); this.tableData = something; this.tableBinding = new BindingSource(); this.tableBinding.DataSource = tableData; this.auxiliaryTableEditorGridView.DataSource = tableBinding; } This works perfectly fine. The problem is this: If a user starts editing

try/catch best practices for optimal performance [duplicate]

廉价感情. 提交于 2019-12-11 05:57:20
问题 This question already has answers here : How slow are .NET exceptions? (13 answers) Closed 6 years ago . I've heard that try/catches are considered to be bad for performance, but that if you're rarely expecting to throw exceptions then they are considered a better way to return failure information rather than using methods/functions with Boolean values. Does using try/catch in this manner better or worse for performance? It certainly makes coding easier. Example: void DoSomething(){ try{

How do I use the generic error codes enum with system_category error codes from <system_error>?

主宰稳场 提交于 2019-12-11 05:46:51
问题 I have this code (very similar to what is suggested here) that throws an exception: int accept4(int sockfd, sockaddr *addr, socklen_t *addrlen, int flags) { const int fd = ::accept4(sockfd, addr, addrlen, flags); if (fd < 0) { const auto tmp = errno; throw ::std::system_error(tmp, ::std::system_category(), "accept4(2)"); } else { return fd; } } And this code for testing for a particular exception reason: catch (const ::std::system_error &e) { static const auto block_err = ::std::system_error

CreateFile Fails for SymLink to File in Parent Folder

落花浮王杯 提交于 2019-12-11 05:46:35
问题 In reference to this question: File.Copy() and Symbolic Links I find that the line SafeFileHandle fileHandle = CreateFile(symlink.FullName, 0, 2, IntPtr.Zero, CREATION_DISPOSITION_OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, IntPtr.Zero); is failing with the error The system cannot find the file specified for the following very specific case: Original file is in a parent directory C:\Temp\SymlinkUnitTest\Original.txt [real file] Symbolic link is in a real subdirectory C:\Temp\SymlinkUnitTest

Where can arbitrary exceptions during WebSocket connections in Autobahn be caught in the Python code?

霸气de小男生 提交于 2019-12-11 05:44:10
问题 I have created a Python program that uses Autobahn to make WebSocket connections to a remote host, and receive a data flow over these connections. From time to time, some different exceptions occur during these connections, most often either an exception immediately when attempting to connect, stating that the initial WebSocket handshake failed (most likely due to an overloaded server), like this: 2017-05-03T20:31:10 dropping connection to peer tcp:1.2.3.4:443 with abort=True: WebSocket

error handling in android. exception Caused by: java.lang.IllegalArgumentException: provider=gps

ぃ、小莉子 提交于 2019-12-11 05:37:43
问题 I've received an error report for one of my apps. I'm pretty much a beginner in android development, and don't quite understand where this could originate from. Where is the app code would it be best to catch all errors and for example just toast message that an error occured? My app basically sits and waits for GPS and/or Network location before displaying a button. This is the error: java.lang.RuntimeException: Unable to resume activity {fr.mcnamara.irelandtravelguide/fr.mcnamara

Sometimes get a ValueError from passing a float to int() method, but not always

流过昼夜 提交于 2019-12-11 05:36:03
问题 Python newbie here. I've been messing around with flow control and have come across a situation I don't quite understand related to exceptions. In the following code I want the user to enter an integer in a given range and, in the case of non-compliance, keep asking them until they give a valid value: while True: try: x = int(raw_input("Pick an integer between 2 and 9. ")) except ValueError: print "Please enter an integer!" continue else: if 2 <= x <= 9: print("Your number is {0}.").format(x)

Best practice: throwing Exception or using Validator on User Input

﹥>﹥吖頭↗ 提交于 2019-12-11 05:33:54
问题 Recently I've been into multiple arguments on whether to throw an exception on false User Input. Example: I'm trying to login though my account is not activated. As the programmer in an OO-language, I could handle this in a few ways. For this case, lets stick to these two: Throw a custom Exception from the local Service with a representative way, extending Exception . Catching this in the class handling User Input. Use a Validator to call the local Service to check whether this account is