0

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?

user3616181
  • 975
  • 2
  • 8
  • 27
  • Personally, while annoying some times, I prefer Java's approach, as it is declarative, you know in advance what a method might do and control how you might handle it - IMHO – MadProgrammer Nov 11 '14 at 19:29
  • I think so too, so I was wondering is it possible to get this kind feature in C#... – user3616181 Nov 11 '14 at 19:34
  • 1
    Why? Because the languages were developed by different persons. Which one? The one you're the most comfortable with. Push C# to work as Java? Code in java instead. By the way the question is pretty broad and opinion-based. If you want good answers, focus on one point (and not the 'why did is X working that way?', because we don't know what happened in their development process). – Pierre-Luc Pineault Nov 11 '14 at 19:36
  • Which is better, apples or oranges? – Servy Nov 11 '14 at 19:38
  • Not including equivalent of Java checked exceptions in C# and ways around it already discussed multiple times on SO. Linked [duplicate](http://stackoverflow.com/questions/3465465/how-to-use-java-style-throws-keyword-in-c) will provide you with link to the discussion [The Trouble with Checked Exceptions](http://www.artima.com/intv/handcuffs.html) by [Anders Hejlsberg](http://en.wikipedia.org/wiki/Anders_Hejlsberg). – Alexei Levenkov Nov 11 '14 at 19:42
  • There are 2 types of exceptions in Java: Checked and Unchecked exceptions. Checked Exceptions should be caught and handled while Unchecked Exceptions raise from programmer's errors and should be treated as logical errors. Java says that a method caller should be aware of exceptional behavior of the calling method and should decide about what should be done. Java style exception handling keeps programmer aware of what can go wrong. Dude! Error handling in OOP uses Exception Handling concept. You can't write codes while you are not aware of possible errors! – Kamran Amini Nov 11 '14 at 19:43
  • @Servy Without any doubt, apples are the dominant fruit-master race here. Even [the internet says so](http://www.googlefight.com/index.php?lang=en_GB&word1=apples&word2=oranges). – Pierre-Luc Pineault Nov 11 '14 at 19:43

1 Answers1

1

Since the exception does not form a part of the method declaration in C#, any exception can be thrown by any method without the method declaring so. Thus, an exception never requires handling since it is never required to be declared.

It's all about design decisions. You could ask yourself a time ago, why Java had not Lambda Expressions.

Icaro Camelo
  • 382
  • 1
  • 10
  • 1
    IMO Java is safer because programmer is aware of possible exceptions that aren't handled in method... But probably... _it depends_ ;) On the other hand, it is sometimes really annoying – user3616181 Nov 11 '14 at 20:00