Files
c-sharp/kivetelek/Program.cs

89 lines
3.0 KiB
C#
Raw Normal View History

2022-03-26 07:34:16 +01:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2022-03-28 10:13:55 +02:00
using System.IO;
2022-03-26 07:34:16 +01:00
namespace kivetelek {
internal class Program {
2022-03-28 10:13:55 +02:00
public void fajl_letrehozas() {
try {
StreamWriter writer = new StreamWriter(@"test.txt", false, Encoding.Default);
writer.WriteLine("sor1");
writer.WriteLine("sor2");
writer.Close();
} catch (Exception e) {
throw;
}
}
2022-03-26 07:34:16 +01:00
static void Main(string[] args) {
//try catch blokk
int[] arr = new int[2];
try { //a tryba irjuk az a kod (reszletet) ahol a kivetel elofordulhat
arr[2] = 1;
} catch (IndexOutOfRangeException e) { //a catch ag akkor fut le ha megtortenik a kivetel, itt adjuk meg mi torjtenjen
Console.WriteLine(e.Message); //miutan megtortent a kivetel
//nem kotelezo megadni feltetelt csak ha egyeni feltetelunk van
//vagy egy konkret feltetelt keresunk
}
//mi magunk is letrehozhatunk kivetelt
try {
throw new Exception("dobott kivetel");
} catch (Exception e) {
Console.WriteLine(e.Message);
}
try {
int[] array = new int[2];
array[3] = 10;
} catch (System.IndexOutOfRangeException) { //tobb catch agat is lehet egy try blokkhoz irni
Console.WriteLine("OutOfRange"); //itt figyelni kell arra milyen sorrendbe irjuk oket
} catch (System.Exception) {
Console.WriteLine("Exception");
}
//sajat kivetel
try {
throw new MyException("sajat kivetel dobasa");
} catch (Exception) {
throw;
}
2022-03-28 10:13:55 +02:00
//Leggyakrabban elofordulo kivetelek
//FileNotFoundException
//Ez a kivetel akkor fordul elo amikor a beolvasott fajl:
//nem letezik vagy rossz eleresi utvonalat adtunk meg
//ahhoz hogy hasznaljuk ezt a kivetelt importalni kell a System.IO-t
2022-03-26 07:34:16 +01:00
2022-03-28 10:13:55 +02:00
//fajl_letrehozas();
try {
StreamReader reader = new StreamReader("test.txt");
string s = reader.ReadLine();
} catch (FileNotFoundException e) {
Console.WriteLine("A falj nem talalhato!");
}
//IndexOutOfRangeException
//Ez akkor fordul elo amikor az index tulmegy a tomb hatarain
2022-03-26 07:34:16 +01:00
Console.ReadKey();
}
class MyException : Exception {
public MyException() { }
public MyException(string message) : base(message) { }
public MyException(string message, Exception innerException) : base(message, innerException) { }
2022-03-28 10:13:55 +02:00
}
2022-03-26 07:34:16 +01:00
}
}