c# - try/catch block not catching an exception -
I'm having a problem where an attempt is not being caught by the hold / hold block. . Exeter reader () , though it is never caught, I am running in debug mode and I have made some suggestions regarding debugger settings.
Let me tell you that I am using SQLite as my provider, and I can see that it throws a SQLiteException, though the problem remains what will be a specific scenario where an exception Have not been caught? (With the exception of stackoverflow exception, threaded exceptions etc ...)
Public INMMerable & lt; Dynamic & gt; Query (string sql, params object [] parameters) {try {return QueryCore (sql, parms); } Catch (Exceptional Exceptions) {New DBE Expansion (SQL, Parameters, X); }} Private IEnumerable & lt; Dynamic & gt; QueryCore (String SQL, Paramaj Object [] Parameters) {{var connection = Create connection ()) {(using var command = QuickComp (SQL, Connection, Parameters)) ({var reader = command.ExecuteReader ()) { While (reader.read ()) {yield returns reader.Toxpando (); }}}}
I also want to add that if I produce a right query against the database, I get the results back, but when I break the query I'm throwing an exception, though not caught.
This is because you are returning the data from the yield keyword. This data makes the actual data method only to run when its results are calculated.
You probably do not want to do this, especially when the results will be read twice (such as two different foreach loops) twice to make you do it to math and catch the exception so Can:
public IEnumerable & lt; Dynamic & gt; Query (string sql, paragraph object [] parameters) {try {return queryCore (sql, parms). ToArray (); } Catch (Exceptional Exceptions) {New DBE Expansion (SQL, Parameters, X); }}
The yield is good for situations where something takes some time, and you do not want to get all those things before you loop through them Able to do So one more possible solution, which may be better for your code readability (which I think does not require to be generated) will be:
public IEmable & lt; Dynamic & gt; Query (string sql, params object [] parameters) {try {return QueryCore (sql, parms); } Catch (Exceptional Exceptions) {New DBE Expansion (SQL, Parameters, X); }} Private IEnumerable & lt; Dynamic & gt; {Var reader = command.ExecuteReader ()) using {QueryCore (String SQL, Ultimate Object Parameters) {{var connection = CreateConnection ()} ({var command = CreateCommand (sql, connection, parms)) {var results = New list & gt; Dynamic & gt; (); While (reader.Read ()) {results.Add (reader.ToExpando ()); } Return results; }}}}
Comments
Post a Comment