Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checked and unchecked exception in .NET

Tags:

java

c

c#

.net

unix

a strange idea comes to me when I'm reading APUE(Advanced Programming in UNIX Environment).

It seems that in UNIX's error handling, there are two types of errors (FATAL & INFATAL). I feel like it's something related to checked and unchecked Exceptions in JAVA.

So, to sum up, in a program, you have two kinds of errors, one of them is critical and will make system crash and you can do nothing about it. Another one is more like a signal that you can catch and do something to fix it.

I heard that in C# there is no checked and unchecked exception, so does C# not have a concept of critical and uncritical errors? Just got very curious because i think this concept is very fundamental.

Update: What is the exception design in other languages? Can anyone talk about this?

like image 760
FrostNovaZzz Avatar asked Dec 13 '22 19:12

FrostNovaZzz


1 Answers

In Java, checked and unchecked exceptions don't exactly map to a fatal or non-fatal error. A checked exception explicitly is stating that the exception may be thrown and someone must catch it (to try to handle it or throw it up the stack), but there is no guarantee that the error may not be fatal (i.e. a syntax error in an SQL query will throw an SQLException and will likely be fatal, but it is a checked exception). An unchecked exception just means that someone doesn't need to catch it, but you still can if you want. It would typically indicate a programming error. Java Errors typically indicate things that are unrecoverable problems (such as an OutOfMemoryError).

The C# design of unchecked exceptions just means you don't need to catch the exceptions, and if uncaught will crash the application. Checked vs unchecked exceptions has been a long standing debate in the development community and there are pros and cons to both. Typically though, you can't do something about an exception and it frequently just ends up getting logged rather than handled, so C# made exceptions unchecked. When you can handle them (for example, if you want to retry an IO operation), you can still catch them and retry.

like image 179
Jeff Storey Avatar answered Dec 15 '22 09:12

Jeff Storey