Why isnt this exception caught? What am I doing wrong

View: New views
2 Messages — Rating Filter:   Alert me  

Why isnt this exception caught? What am I doing wrong

by alanb :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I have written some code to save a file to the fs. See code below.

        string fullpath = System.IO.Path.Combine(base_directory,filename);
        StreamWriter sw = new StreamWriter(fullpath);
        try
        {
                sw.Write(generator.CodeText);
        }
        catch (Exception ex)
        {
                Console.WriteLine(ex.Message);
        }
        finally
        {
                sw.Close();
        }

All works fine when the path is correct but when wrong the catch is ignored and I get

Exception in Gtk# callback delegate
  Note: Applications can use GLib.ExceptionManager.UnhandledException to handle the exception.
System.IO.DirectoryNotFoundException: Directory not found
...

So please can someone tell me what is happening and why the catch above is being ignored. I am using mono 1.91,
gtk-sharp2 2.12.0-2ububtu3 and using monodevelop 2.0alpha as my ide.
thanks
alan

Re: Why isnt this exception caught? What am I doing wrong

by Chris Howie :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sun, Aug 10, 2008 at 6:41 PM, alanb <alan.battersby@...> wrote:
> So please can someone tell me what is happening and why the catch above is
> being ignored.

The exception is being thrown in the StreamWriter constructor, which
is outside of your try block.

--
Chris Howie
http://www.chrishowie.com
http://en.wikipedia.org/wiki/User:Crazycomputers
_______________________________________________
Mono-list maillist  -  Mono-list@...
http://lists.ximian.com/mailman/listinfo/mono-list