-5

I Need to write a Try-Catch-Finally. First of all im new to Programming. Back to the Problem.

In the Try-Block i want to open a Text that doesn't exist.

in the Catch-Block a Messagebox should show up with the FileNotFoundException.

and I still dont know what I should put in the Finally-Block.

try
{
   FileStream File = new FileStream("beispiel.txt", FileMode.Open);
}
catch (FileNotFoundException fnfex)
{
   //MessageBox with fnfex
}
finally
{
   //idk
}

Thanks

Fabjan
  • 13,506
  • 4
  • 25
  • 52
hrv-ilija
  • 13
  • 2

5 Answers5

0

Finally is code that always executes. I would say that it is a common pattern to use the finally block to clean up anything that could exist. For example, if you have the filestream...

Excuse me if the types are not correct, I do not have C# currently but the pattern remains...

FileStream file;

try {
    file = new FileStream("example.txt", FileMode.Open);
    file.open();
}
catch (Exception ex) {
    //message box here
}
finally {
    // Clean up stuff !
    if (file != null) {
        file.close()
    }
}
Tyler Nichols
  • 196
  • 3
  • 14
0

Common usage of catch and finally together is to obtain and use resources in a try block, deal with exceptional circumstances in a catch block, and release the resources in the finally block. For further information take a look at https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch-finally

As you just want to catch the exception and then print out a message, you could simply get rid of the finally block, like that:

try
{
   using (FileStream File = new FileStream("beispiel.txt", FileMode.Open)){}
}
catch (FileNotFoundException fnfex)
{
   //MessageBox with fnfex
   MessageBox.Show(fnfex.Message);
}

The using statement ensures that the object is disposed as soon as it goes out of scope, and it doesn't require explicit code or a finally to ensure that this happens.

oRole
  • 1,316
  • 1
  • 8
  • 24
0

Finally is used to have a guaranteed way of doing something, even if there is an exception thrown. In your case, you could for example dispose your stream, but generally it's better to use using statement for objects that implement IDisposable, so you can just do

using (var stream = new FileStream(...))
{
   // do what ever, and the stream gets automatically disposed.
}

see:

using statement

IDisposable

tjugg
  • 2,977
  • 2
  • 18
  • 24
0
DialogResult result;

try
{
    FileStream File = new FileStream("beispiel.txt", FileMode.Open);
}
catch (FileNotFoundException fnfex)
{
    result =
        MessageBox.Show(
            this, 

            // Message: show the exception message in the MessageBox
            fnfex.Message, 

            // Caption
            "FileNotFoundException caught", 

            // Buttons
            MessageBoxButtons.OK,
            MessageBoxIcon.Question, 
            MessageBoxDefaultButton.Button1, 
            MessageBoxOptions.RightAlign);
}
finally
{
    // You don't actually NEED a finally block

}
Omar Himada
  • 2,540
  • 1
  • 14
  • 31
-1

Other than what people already said about the try...catch...finally block, I believe what you're looking for is

try {
    file = new FileStream("example.txt", FileMode.Open);
    file.open();
}
catch (Exception ex) {
    MessageBox.Show(ex.Message);
}

But you need to add reference to System.Windows.Forms in your project and add the using statement to your class

using System.Windows.Forms;

Or, if you just want to display the message in the console, you can use

Console.WriteLine(e.Message);

and forget the reference

Bruno Avelar
  • 670
  • 4
  • 16