I am a C# developer doing occasional coding in Java. Can someone explain in simple terms what are checked exceptions in Java and why is it needed? Haven't come across this term in C#.
-
possible duplicate of Why must only certain Exceptions be declared as thrown in method signatureOhadR– OhadR2014-04-20 16:04:38 +00:00Commented Apr 20, 2014 at 16:04
-
1checked exceptions in Java is violation of Open/Closed Principle.You must declare that exception in the signature of each method between you and the catch.naeemgik– naeemgik2019-10-31 05:19:21 +00:00Commented Oct 31, 2019 at 5:19
-
For C#, you can check the following link github.com/AminEsmaeily/Portia.Roslyn.CheckedExceptionAmin– Amin2021-08-23 03:56:40 +00:00Commented Aug 23, 2021 at 3:56
3 Answers
Checked exceptions are exceptions that the compiler require you handle in some way.
In Java, checked exceptions are Throwables that are not RuntimeException, Error, or one of their subclasses.
The Java designers felt they were needed to ensure programs handled exceptions that were reasonably likely. A classic example is IOException. Any time a program does I/O, there is a possibility of failure. The disk could be full, the file might not exist, there might be a permissions problem, etc.
Thus, Java is designed such that a program must syntactically handle the exception in some way. This could be with a catch block, or by rethrowing the exception in some way.
C# does not have checked exceptions. They decided to leave this issue up to the application developers (interview). Checked exceptions are controversial because they can make code verbose, while developers sometimes handle them trivially with empty catch blocks. Further, it can be arbitrary which standard library methods throw checked exceptions. For instance, why doesn't File.delete (a new Java 7 API does this differently) throw IOException?
Another concern Hejlsberg noted in that interview is versionability. Adding a checked exception to a throw clause forces all code using that method to be modified and recompiled.
9 Comments
Adding a checked exception to a throw clause forces all code using that method to be modified and recompiled - so you have for unchecked exceptions. Well, except if you think Will fail under some circumstances is how you'd like to describe your API. EVERY exception is part of the public interface of a method, adding ones later is like saying "Oh and from now on that int parameter will have a completely different semantic meaning - but don't worry, it's still binary compatible!"throw Exception in Java - that has quite a stigma on it I think. But yes I'm still hoping for some solution that allows us to cut down on the boilerplate necessary for checked exceptions without the downsides of unchecked ones.In Java, a checked exception (as Matthew Flaschen correctly points out) is an exception that the compiler requires you to handle. These are exceptions that are declared on function definitions (e.g. function bob() throws ImNotBobException { ... } to say that calling that function may throw that exception - e.g. NumberFormatException when parsing an integer, or IOException when writing to a file.
However, some exceptions can be thrown from unknown or unexpected places that are simply impractical to handle on every level, so the compiler does not require you to handle these. These are unchecked exceptions. They can be thrown from various places that do not declare to throw them (often by attempting to call a method on an object when that object has not been initialised yet, i.e. is null - this will result in a NullPointerException.)
Hope this helps.
1 Comment
Checked exceptions are exceptions that require any "consuming" class to have to code that explicitly checks (and hopefully handles) the exception.
For example, if the Apple class has an Eat() method which includes the checked exception of WormFound then any code that calls that method will need to explicitly have a catch for that exception.
As a side note, it is feature of Java and not of C#.
(At the point when C# was created, the pros of checked exceptions weren't so obvious in the eyes of the C# team so they weren't included.)