System Exceptions Comparison — TR

M. Esat Ceber
3 min readMay 16, 2024

--

Bu yazımda sizlere JAVA ve .NET üzerinde en sık görülen exception’ların teknolojilere göre nasıl alındığını ve farklılıklarını göstermeye çalıştım.

Null Argument:

Bir metodun veya fonksiyonun beklenen bir parametreye null olarak bir argüman aldığında oluşur.

public void sayHello(String name) {
System.out.println("Hello, " + name);
}
// NullPointerException
sayHello(null);
public void SayHello(string name)
{
Console.WriteLine("Hello, " + name);
}
// ArgumentNullException
SayHello(null);

Invalid State:

Bir nesnenin veya sistemin geçerli bir durumda olmadığı bir durumda bir işlem yapmaya çalıştığınızda ortaya çıkar.

public void doOperation() {
if (!isValidState()) {
throw new IllegalStateException("Invalid state: operation cannot be performed in current state");
}
// Perform the operation if the state is valid
}
public void DoOperation() {
if (!IsValidState) {
throw new InvalidOperationException("Invalid state: operation cannot be performed in current state");
}
// Perform the operation if the state is valid
}

File Not Found:

Belirtilen dosyanın bulunamadığı bir durumda ortaya çıkar.

public void readFile(String filePath) throws FileNotFoundException {
File file = new File(filePath);
if (!file.exists()) {
throw new FileNotFoundException("File not found: specified file does not exist");
}
// Perform the reading operation if the file exists.
}
public void ReadFile(string filePath) {
if (!File.Exists(filePath)) {
throw new FileNotFoundException("File not found: specified file does not exist");
}
// Perform the reading operation if the file exists.
}

Input/Output Error:

Giriş veya çıkış işlemleri sırasında bir hata meydana geldiğinde ortaya çıkar.

public void readDataFromFile(String filePath) throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
// Perform the operation of reading data from the file
} catch (IOException e) {
throw new IOException("Input/output error: an error occurred during input/output operation", e);
}
}
public void ReadDataFromFile(string filePath) {
try {
string data = File.ReadAllText(filePath);
// Perform the operation of reading data from the file
} catch (IOException e) {
throw new IOException("Input/output error: an error occurred during input/output operation", e);
}
}

Format Error:

Belirli bir verinin beklenen biçime uymadığı bir durumda ortaya çıkar.

public int parseInt(String str) {
try {
return Integer.parseInt(str);
} catch (NumberFormatException e) {
throw new NumberFormatException("Format error: string cannot be parsed as a number");
}
}
public int ParseInt(string str) {
try {
return int.Parse(str);
} catch (FormatException e) {
throw new FormatException("Format error: string cannot be parsed as a number");
}
}

Unsupported Operation:

Belirli bir işlemin mevcut koşullarda desteklenmediği bir durumda ortaya çıkar.

public void doOperation() {
throw new UnsupportedOperationException("Unsupported operation: current operation is not supported");
}
public void DoOperation() {
throw new NotSupportedException("Unsupported operation: current operation is not supported");
}

Out of Memory:

Belleğin tükendiği bir durumda ortaya çıkar.

public void allocateMemory(int size) {
try {
Object[] array = new Object[size];
} catch (OutOfMemoryError e) {
throw new OutOfMemoryError("Out of memory: insufficient memory to allocate object");
}
}
public void AllocateMemory(int size) {
try {
object[] array = new object[size];
} catch (OutOfMemoryException e) {
throw new OutOfMemoryException("Out of memory: insufficient memory to allocate object");
}
}

Aggregate Exception:

Birden fazla istisnanın bir Task veya Task<T> nesnesi tarafından işlenmesi sırasında ortaya çıkar. Java’da bu tür bir exception bulunmuyor.

public void DoTasks(List<Task> tasks) {
try {
Task.WaitAll(tasks.ToArray());
} catch (AggregateException e) {
throw new AggregateException("One or more errors occurred", e.InnerExceptions);
}
}

--

--

M. Esat Ceber

DevOps & SRE expert, improving Large-Scale Systems' software. Excited to share insights in articles. Let's connect!