I wrote two corresponding codes in C# and Java:
class Class
{
public static void f()
{
throw new Exception("error");
}
}
class Program
{
static void Main(string[] args)
{
Klasa.f();
}
}
class Class {
public static void f() throws Exception {
throw new Exception("error");
}
}
class Program {
public static void main(String[] args) {
try {
Klasa.f();
} catch (Exception e) {
e.printStackTrace();
}
}
}
In Java we are obligate to throw exception outside the method (if it's not handled in it) and catch an exception while using this kind of methods. On the other hand, C# doesn't forced us to do so. Why? Which approach is better? Can we push C# to works as Java here?