You searched for exceptions. We found 168 results in 0.250 seconds.

C# use moq to throw exception from async method

I am using the Moq library as a mocking framework together with nunit. I am having trouble to figure out how to setup my mock object to throw an exception from an async method...
Jon Skeet
people
quotationmark

Async methods don't normally throw exceptions directly - they return tasks which end up being faulted. The simplest way to create such a task is to use Task.FromException. You haven't given many details in your question, but I suspect if... more

people

JSON is valid, but result in code returns null

I have this simple JSON { "persons": [{ "firstname": "Brad", "lastname": "Pitt" }, { "firstname": "George", "lastname": "Clooney" },...
Jon Skeet
people
quotationmark

Your data doesn't contain a List<PersonObject> - it contains a single PersonObject which in turn contains a List<Person>. So this works fine: var json = File.ReadAllText("test.json"); var obj =... more

people

Empty catch but exception is still thrown

Have a look at the following source code private IEnumerable<string> GetAntivirusSoftwareFromNamespace(string @namespace) { try { var...
Jon Skeet
people
quotationmark

The exception isn't thrown within the method you've shown - you're basically returning a query to the caller, and that query is then failing. One option would be to force the query to execute immediately by calling ToList(). That way, any... more

people

Avoiding skipping a row By next() Method of ResultSet

This is a simple code print some rows from a Database. but When I execute this nothing is print on screen. I figured that the problem is rs.next() method is skipping a row. So How...
Jon Skeet
people
quotationmark

First, stop building SQL like that - use parameterized SQL and a PreparedStatement. Your current code is vulnerable to SQL injection attacks. Basically, don't call rs.next() twice in a row (first in the if then in the while)... you can... more

people

Code throwing exception. Where is the error?

I'm learning C and C#, this question is for C#. I can't see where and what is throwing the exception. Any help much appreciated? Here is the code: private static void...
Jon Skeet
people
quotationmark

I can't immediately tell you where the problem is, but I can tell you how to find out. First, remove this catch block (and indeed the try clause): catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } That is... more

people

NullReferenceException was unhandled in C# Timer TweetSharp

I am working on a twitter project that lets user to auto-tweet to the mentioned tweets using C#. I am using TweetSharp with C#. The problem is, when I try to refresh the form_load...
Jon Skeet
people
quotationmark

I've come across exactly this problem in the last week. I strongly suspect that it is related to access tokens expiring or other errors: I've regularly seen this occur after my program has been active for 5 minutes (of polling every 20... more

people

Why does nothing write to my file?

I have a program that I am working on and I have an issue with writing a string into my file. The file is created, but the string value that should be written into it ultimately...
Jon Skeet
people
quotationmark

I strongly suspect you're getting exceptions logged - you should improve your exception handling anyway, but if you're going to log exceptions, you really need to look in the logs when things don't work! This is almost certainly the... more

people

Why is catching a RuntimeException not considered a good programming practice?

Why is catching a RuntimeException using catch(Throwable exc) {} not considered a good programming practice? What is the right way to handle RuntimeExceptions? Also, why does...
Jon Skeet
people
quotationmark

Usually, a RuntimeException indicates a programming error (in which case you can't "handle" it, because if you knew to expect it you'd have avoided the error). Catching any of these general exceptions (including Throwable) is a bad idea... more

people

IList.Select() that returns an IList (without copying)

I have a method with an IList<T> parameter. It's IList and not IEnumerable, because the method needs quick random access and most entries will not be queried at all (the...
Jon Skeet
people
quotationmark

No, there's nothing within the framework that does this as far as I'm aware. Your second option seems sensible to me. You'll need to make it read-only, throwing appropriate exceptions for the mutating operations of IList<T>. One... more

people

Why doesn't catching Exception catch RuntimeException?

This is very odd to me. RuntimeException inherits from Exception, which inherits from Throwable. catch(Exception exc) { /* won't catch RuntimeException */ but catch(Throwable...
Jon Skeet
people
quotationmark

The premise of the question is flawed, because catching Exception does catch RuntimeException. Demo code: public class Test { public static void main(String[] args) { try { throw new RuntimeException("Bang"); ... more

people