Hataları, projelerde olası sorunlarda projenin çalışmaya devam etmesi ve hatayı kullanıcıya göstermek için kullanırız
Hata içeriğini kullanıcıya doğrudan vermek sistem açıklarına sebep olabilir
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exceptions
{
class Program
{
static void Main(string[] args)
{
/*
* Hataları, projelerde olası sorunlarda projenin çalışmaya devam etmesi ve hatayı kullanıcıya göstermek için kullanırız
* Hata içeriğini kullanıcıya doğrudan vermek sistem açıklarına sebep olabilir**/
//ExceptionIntro();
//Creating My Exception Class
try
{
Find();
}
catch (RecordNotFoundException exception)
{
Console.WriteLine(exception.Message);
}
//Action Delegasyonu
//()=> { } , () kısmı methoda karşılık geliyor parametresiz birşey göndereceğim, => (lamda) o kod bloğunun karşılığı da, {} kod kümesi
//Method içerisinde Method gönderme
HandleException(()=>
{
Find();
}); // Action Delegation Method içeriisinde parametre olarak method göndermemizi sağlar
Console.ReadLine();
}
private static void HandleException(Action action)
{
try
{
action.Invoke();
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
}
private static void Find()
{
List<string> students = new List<string> { "Mehmet", "Ahmet", "Ali" };
if (!students.Contains("Ayşe"))
{
throw new RecordNotFoundException ("Record not found");
}
else
{
Console.WriteLine("Record Found!");
}
}
private static void ExceptionIntro()
{
string[] students = new string[3] { "Mehmet", "Burak", "Ali" };
try
{
students[3] = "Ahmet";
}
catch (IndexOutOfRangeException exception)
{
Console.WriteLine(exception.Message);
}
catch (DivideByZeroException exception)
{
Console.WriteLine(exception.Message);
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
Console.WriteLine(exception.InnerException); ///Hata hakkında daha detaylı bilgi verir.
}
}
}
}
RecordNotFoundException.cs (Kendi oluşturduğumuz hata sınıfı)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exceptions
{
public class RecordNotFoundException : Exception
{
public RecordNotFoundException(string message):base(message)
{
}
}
}

Yorum bırakın