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

How does this Java code snippet work? (String pool and reflection)

Java string pool coupled with reflection can produce some unimaginable result in Java: import java.lang.reflect.Field; class MessingWithString { public static void main...
Jon Skeet
people
quotationmark

What happened to Mario ?? You changed it, basically. Yes, with reflection you can violate the immutability of strings... and due to string interning, that means any use of "Mario" (other than in a larger string constant expression,... more

people

Async method to return true or false in a Task

I know that an async method can only return void or Task. I have read similar methods for Exception handling inside async methods. And I'm new to async programming so I am looking...
Jon Skeet
people
quotationmark

It sounds like you just need to return a Task<bool> then: private async Task<bool> RefreshContactsAsync() { try { ... } catch // TODO: Catch more specific exceptions { return false; } ... more

people

Destination file blank after writing with StreamWriter C#

I have written a console application that in itself is working as I would like it to. It's main output works great in console. But the results inside the loop I want written to a...
Jon Skeet
people
quotationmark

When I run that code, I see an exception: Unhandled Exception: System.UnauthorizedAccessException: Access to the path 'c:\Test.txt' is denied. It's not clear how you're running this, but if it's configured as a console app and you run... more

people

Is try/catch without finally bad

So I know that a finally block of code will execute regardless of an exception, but is it bad to not use it? I've been using just try/catch and wanted to know if this was a bad...
Jon Skeet
people
quotationmark

If you don't have anything you need to clean up at the end, that's perfectly fine. finally blocks are almost always about resource cleanup... and there are plenty of times where you want to catch an exception but don't have any resources... more

people

catch exception library not working as expected. What is wrong with this code below?

I have a JUnit 4 Test and I am using https://code.google.com/p/catch-exception/ library @Test public void testInvalidArgument() throws Exception{ verifyException(new...
Jon Skeet
people
quotationmark

I haven't used the library myself, but it seems reasonably clear why your usage would fail: your code for the first statement is equivalent to: Operation op = new Operation("" , "."); verifyException(op,... more

people

SqlConnection.ServerVersion Exception after Close()

I open a SqlConnection. SqlConnection Conn = new SqlConnection(...); Conn.Open(); ... Conn.Close(); Conn.Dispose(); //debugger breakpoint When I look in my debugger at this...
Jon Skeet
people
quotationmark

Just avoid examining the connection object after you've disposed of it. ("Doctor, it hurts when I do this..." "Stop doing that then!") The easiest - and most reliable - way to do that is to use a using statement instead: using (var conn =... more

people

Why Java7 introduces AutoCloseable specially?

AutoCloseable is introduced in jdk1.7 and Cloesable is already in jdk1.5. And According to https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html ...
Jon Skeet
people
quotationmark

Closeable is restricted to throw IOException, which may not be appropriate for some closeable but non-IO-bound resources. AutoCloseable is declared to throw Exception, making it more general-purpose. The API for Closeable can't be... more

people

Exception has been thrown by the target of an invocation (MethodBase.Invoke Method)

I want to catch the exceptions that are thrown in methods called with invoke method. public void TestMethod() { try { method.Invoke(commandHandler, new[] {...
Jon Skeet
people
quotationmark

Yes, you're calling the method via reflection. So as per the documentation, a TargetInvocationException will be thrown if the target method throws an exception. Just use the InnerException property to obtain - and potentially throw - the... more

people

Convert asynchronous action to asynchronous function delegate, preserving synchronous exception delivery

I want to convert asynchronous action delegates to asynchronous function delegates that return a specified value. I have come up with an extension method for this: public static...
Jon Skeet
people
quotationmark

Well, you won't be able to use async in the initial call. That much is clear. But you can use a synchronous delegate which calls the function, and then captures the returned task to await it inside an async delegate: public static... more

people

char array to int array conversion in java generates null pointer exception

I am trying to run the following HillCipher program but it is terminated after the line of conversion of char array to int array and after compilation of that code it shows me...
Jon Skeet
people
quotationmark

You never assign a value to iKey, so it has its initial default value of null - it's as simple as that. You need to create a new array, e.g. // Given that you've hard-coded the length of cKey as well... iKey = new int[2][2]; I'd also... more

people