First chance FileNotFoundException when calling IsolatedStorageFile.OpenFile - silverlight

Somewhere in my code, I have this line:
return _store.OpenFile(path, fileMode);
With fileMode being sometimes FileMode.Create and sometimes FileMode.Open.
Everything works well, I always get a valid stream and if necessary, the file is correctly created.
But, I've just discovered in my VS output that I have the following message every time I call the method where the above line is:
A first chance exception of type 'System.IO.FileNotFoundException'
occurred in mscorlib.dll
I get this error when the file is created and I also get this error when the file is overwritten (and obviously exists).
I'm just curious about these errors since everything works perfectly.
Thanks,
EDIT: Same thing with new IsolatedStorageFileStream(...). Everything works fine but I still get the "first chance exception" message.

var isf = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream isfs;
if (!isf.FileExists(_filename))
isfs = new IsolatedStorageFileStream(_filename, System.IO.FileMode.Create, isf);
else
isfs = new IsolatedStorageFileStream(_filename, System.IO.FileMode.Open, isf);
var writer = XmlWriter.Create(isfs);
xml.Save(writer);
writer.Close();
isfs.Close();
isfs.Dispose();
isf.Dispose();

Found the answer.
It's just how VS debugger works: for every exception caught by a {{catch}} block, a "first chance exception" message appears in the VS output.
So here, we can guess that, internally, the {{OpenFile}} method uses a try/catch block to check if the file exists.

Related

The code is correct yet it needs try-catch.. why is that?

I am very new to programming and I saw this code that runs perfect with try/catch.
I thought then try/catch is not necessary, as all I know that it just for checking errors and to change the message of the compile when in error.
But when I removed it the code renders many errors and refuse to compile. So what is try/catch is really doing here? because
The code:
import java.io.FileWriter;
public class html {
public static void main(String[] args) {
try{
FileWriter fw=new FileWriter("E:\\rrr.html");
fw.write("Welcome to javaTpoint.");
fw.close();
}catch(Exception e){System.out.println(e);}
System.out.println("Success...");
}
The FileWriter class always throws an exception i.e, 'IOException',
Hence, whenever you use it IOException needs to be handled, so the
try-catch block is mandatory while using FileWriter.
What is an IOException?
An IOException is any unexpected problem the JVM encounters while attempting to run a program. Possible problems that it may encounter are:
attempting to read from a file that does not exist
attempting to write to a file that has an invalid name (a slash or a question mark in the title should do it)
attempting to read the next token in a file when there are no more tokens.
When an IOException is thrown, it means that whatever is throwing the exception (perhaps a try{}-catch block that reads data from a file) can throw an IOException, for example if the file is not found, corrupted, etc, or when the file is otherwise unable to be read, or any other of a list of issues that can occur with the IO package and it's extensions.

HtmlUnit parses "http://www.ean-search.org/sitemap.html" with 404 return

I'm trying to get and parse the page "http://www.ean-search.org/sitemap.html", but it always got 404 error and empty page. All the text content area are blank.
I tried many options configuration of HtmlUnit webclient, e.g. .setThrowExceptionOnFailingStatusCode(false), setThrowExceptionOnScriptError(true), setRedirectEnabled(false), setJavaScriptEnabled(true), setThrowExceptionOnScriptError(false).
None of them worked...
Anyone have any suggestion? Thanks.
ps: my webclient code:
myWebClient = new WebClient(BrowserVersion.FIREFOX_3_6);
myWebClient.setIncorrectnessListener(new CustomizedInconnectnessListener());
myWebClient.setTimeout(180000); //3 min, used twice, first for connection, second for retrieval
try {
myWebClient.setUseInsecureSSL(true);
} catch (GeneralSecurityException ex) {
logger.log(Level.SEVERE, "cannot set UseInsecureSSL for BNP webclient",ex);
//ignore it, continue
}
myWebClient.setRedirectEnabled(true);
myWebClient.setCssEnabled(false);
myWebClient.setJavaScriptTimeout(30000); //timeout for executing java script
myWebClient.setThrowExceptionOnScriptError(false);
HtmlPage htmlpage = (HtmlPage) myWebClient.getHtmlPage("http://www.ean-search.org/sitemap.html");
myWebClient.waitForBackgroundJavaScriptStartingBefore(3000);
Thread.sleep(3000);
System.out.println(htmlpage.asXml());
Well, the code you're using looks really bad. I got many errors and warnings... it is not even possible to compile it. Eg myWebClient.getHtmlPage should be myWebClient.getPage.
This code works for me and outputs the content of the page:
WebClient myWebClient = new WebClient(BrowserVersion.FIREFOX_17);
HtmlPage page = myWebClient.getPage("http://www.ean-search.org/sitemap.html");
System.out.println(page.asXml());
Make sure you use the latest HtmlUnit libraries and also pay attention to the compiler when it tells you that something is deprectated. It is not advised to use those method and fields.

HttpWebRequest.BeginGetResponse results in System.NotSupportedException on Windows Phone

I realise that similar questions have been asked before however none of the solutions provided worked.
Examining the token returned from the BeginGetResponse method I see that the following exception is thrown there:
'token.AsyncWaitHandle' threw an exception of type 'System.NotSupportedException'
This page tells me that this exception means the Callback parameter is Nothing, however I'm passing the callback - and the debugger breaks into the callback method when I insert a breakpoint. However the request object in the callback is always null. I can view the same exception detail in the result object in the callback method.
I've tried using new AsyncCallback(ProcessResponse) when calling BeginGetResponse
I've tried adding request.AllowReadStreamBuffering = true;
I've tried this in-emulator and on-device, with no luck on either.
public static void GetQuakes(int numDays)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://magma.geonet.org.nz/services/quake/geojson/quake?numberDays=" + numDays);
// Examining this token reveals the exception.
var token = request.BeginGetResponse(ProcessResponse, request);
}
static void ProcessResponse(IAsyncResult result)
{
HttpWebRequest request = result.AsyncState as HttpWebRequest;
if (request != null)
{
// do stuff...
}
}
So I'm at a bit of a loss as to where to look next.
'token.AsyncWaitHandle' threw an exception of type
'System.NotSupportedException'
This page tells me that this exception means the Callback parameter is
Nothing
The documentation you are looking at http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetresponse%28v=vs.95%29.aspx is for BeginGetResponse. Silverlight does not use the AsyncWaitHandle, and correctly throws a NotSupportedException. You are seeing the exception System.NotSupportedException is for call to IAsyncResult.AsyncWaitHandle you are making when you inspect token.
The documentation on IAsyncResult.AsyncWaitHandle says explicitly that it is up to the implementation of IAsyncResult whether they create a wait handle http://msdn.microsoft.com/en-us/library/system.iasyncresult.asyncwaithandle(v=vs.95).aspx. Worrying about this is sending you down th wrong path.
I think you need to descibe the actual problem you are seeing. It is great to know what you have investigated, but in this case it does help resolve the problem.
The code should work and in ProcessResponse request should not be null when you test it in the if statement. I just copied the code you have provided into a windows phone application and ran it with no problems.

Can't read files from BlobStore

I have been trying to write and read files directly from the BlobStore, but it just doesnt work.
The issue is I open the file like file = fileService.getBlobFile(blobKey); and it doesn't throw any exception but right in the next line I call readChannel = fileService.openReadChannel(file, false); and that one throws a FileNotFoundException.
I'm confused as to why the first line did not throw the exception.
Here is the same issue
Unfortunately no one answered that question.
I haven't had any problems with writes or deletes, but I too get a FileNotFoundException when using openReadChannel(...) with an AppEngineFile.
I've tried using an AppEngineFile created from its constructor taking a complete path. I've tried using an AppEngineFile obtained from getBlobFile(...) like you do above. Either way, when the AppEngineFile is passed to openReadChannel(...) a FileNotFoundException is thrown.
My workaround was to let BlobstoreService.serve(...) do all the work of reading and sending the file. I suspect that using the FileService to read from an AppEngineFile isn't supported yet (I'm using 1.6.0), so reads must be done via the BlobstoreService (serve(...), fetchData(...), BlobstoreInputStream).

loading pictures from a folder on loading time

hello i am trying to load images from files in the start of my program and for some unknown reason
whenever i use those lines somehow i am getting thrown out of my loading function
when i press on a button not during the load of the program it does work and i am able to load pictures
this is my loading pictures code :
Image pic = new Image();
string imagePath = String.Format(#"Images\{0}", 1); // this is ofc a file which is inside my debug
pic.Source = new BitmapImage(new Uri(imagePath)); // folder
more info: when i am trying to put this line in my constructor i am getting for some reason an exception:
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: 'The invocation of the constructor on type 'yad2.PresentationLayer.MainWindow' that matches the specified binding constraints threw an exception.' Line number '5' and line position '9'.
thanks in advance for your help
"Images\1" is not a valid URI. You can create the Uri by using the FileInfo class:
FileInfo fi = new FileInfo(imagePath);
Uri uri = new Uri(fi.FullName);
pic.Source = new BitmapImage(uri);
Also, a tip to help you debug exceptions in code-behind: Open the Exceptions window (ctrl+alt+e) and check both boxes for Common Language Runtime Exceptions. This will cause execution to break when the error occurs, making it a lot easier to work out what the problem is.

Resources