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

Will c# compiler optimize empty if blocks

Is there a chance that C# will optimize the following code block? if (specField == null || AddSystemType(specField, layout) || AddEnumType(specField,...
Jon Skeet
people
quotationmark

Well you can use ildasm to see what the compiler has optimized for yourself. But if you were expecting it to remove the code entirely, it can't - because those three method calls could throw exceptions or modify state. So the best it could... more

people

How to read whole the text in text file except last line?

I have write a code to print the whole text in text file but i couldn't know how to enable it to read the whole text except last line The Code : public class Files { /** *...
Jon Skeet
people
quotationmark

The simplest approach is probably to print the previous line each time: String previousLine = null; String line; while ((line = reader.readLine()) != null) { if (previousLine != null) { System.out.println(previousLine); } ... more

people

How good a practice is this?

Say I have a Java application which handles thousands of users. So for each user action a certain block of code will be executed repeatedly and I am creating millions of temporary...
Jon Skeet
people
quotationmark

You should absolutely let Java handle this on its own. All you're doing is making your code harder to read and maintain. All the variables you're nulling out are going out of scope at the end of the method anyway - and the GC knows... more

people

Windows 8 store app XmlWriter does not write anything to file

I am not very well versed with .NET's XML libraries and trying to wrap my head around IXmlSerializable interface. I am developing a windows store app (W8) and wish to use the...
Jon Skeet
people
quotationmark

Given the comment thread - this was about not closing all the resources. You're disposing fs (manually, if no exceptions are thrown) but not x. You should really use using statements for everything like this: using (var stream = await... more

people

If parent method throws checked exception, then is it compulsory to throw the same exception in child class?

Given: static class A { void process() throws Exception { throw new Exception(); } } static class B extends A { void process() { System.out.println("B "); } } public...
Jon Skeet
people
quotationmark

The point is that the compiler doesn't know that you're calling an overridden method which doesn't throw any checked exceptions. When it sees: a.process(); it doesn't "know" that the value of a is actually a reference to an instance of... more

people

Potential sync issues with Invoke?

I am trying to read value from control in worker thread. public void worker() { while (true) { ewh.WaitOne(); int index = -1; ...
Jon Skeet
people
quotationmark

As I understand Invoke is launched asynchronously. No, Invoke is synchronous, in that it will block until the delegate has completed in the UI thread. It's unfortunate that this isn't clearly documented in Control.Invoke(Delegate).... more

people

Adding return in finally hides the exception

I have the following code public static void nocatch() { try { throw new Exception(); } finally { } } Which gives the error Exception in...
Jon Skeet
people
quotationmark

The error disappears because your code is now valid. (Not nice, but valid.) If a finally block just has a straight return; statement, then the overall try/catch/finally or try/finally statement can't throw any exceptions - so you don't... more

people

Get file that PrintWriter writes to

How do you get the name of the file that a PrintWriter object is writing to? PrintWriter myPrintWriter = new PrintWriter(fileName, "UTF-8"); I need the fileName parameter.
Jon Skeet
people
quotationmark

You can't - it might not even be writing to a file, e.g. StringWriter stringWriter = new StringWriter(); PrinterWriter printWriter = new PrintWriter(stringWriter); If you need this information, you could potentially create a subclass of... more

people

list all property names of controls based on condition in C#

I am trying to parse through all properties of all my controls (for example ascx files) from a certain page (this.Page.Controls) in order to obtain the name of the property that...
Jon Skeet
people
quotationmark

This is the documented behaviour of HtmlContainerControl.InnerHtml - in the "exceptions" section, it's documented that it will throw HttpException if: There is more than one HTML server control. - or - The HTML server control is... more

people

c# return that also returns the calling function similar to PHP's die() early out

Note: This is a question from the PHP perspective for C#. Maybe it isn't the right way to do it, but here is what I'm trying to do. Suppose you have defined: void Die(string...
Jon Skeet
people
quotationmark

Not without throwing an exception, no. Bear in mind that in many cases the calling method will be non-void - in that case, what value would you expect it to return? The use case you're describing does sound like it would be better handled... more

people