PDA

View Full Version : raise exception



titbasoft
یک شنبه 21 فروردین 1384, 12:11 عصر
در net. چطوری میشه یک exception ساخت و آن را raise کرد؟ یه چیزی تو مایه های err.raise خودمان.

M.GhanaatPisheh
یک شنبه 21 فروردین 1384, 16:05 عصر
Throw


// throw example
using System;
public class ThrowTest
{
public static void Main()
{
string s = null;

if (s == null)
{
throw(new ArgumentNullException());
}

Console.Write("The string s is null"); // not executed
}
}



using System;
using System.IO;
public class ProcessFile
{
public static void Main()
{
FileStream fs = null;
try
//Opens a text tile.
{
fs = new FileStream("data.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs);
string line;
//A value is read from the file and output to the console.
line = sr.ReadLine();
Console.WriteLine(line);
}
catch(FileNotFoundException e)
{
Console.WriteLine("[Data File Missing] {0}", e);
throw new FileNotFoundException("[data.txt not in c:\\dev directory]",e);
}
finally
{
fs.Close();
}
}
}

titbasoft
یک شنبه 21 فروردین 1384, 16:32 عصر
مرسی ، :flower: