Questions tagged [checked-exceptions]

The exceptions that need to be declared in a method or constructor's `throws` clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.

The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.

The class Exception and any subclasses that are not also subclasses of RuntimeException are checked exceptions. Checked exceptions need to be declared in a method or constructor's throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.

Details: http://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html

145 questions
2
votes
1 answer

Why throws upcasting checked exception

I notice that methods in some sophisticated libraries, including the standard JDK, tend to throws upcasting exception. At first I found this phenomenon in the source code of Apache POI, later see it again in java.io.FileWriter like this: public…
SedriX
  • 500
  • 3
  • 10
2
votes
1 answer

Using Checked Exceptions vs Unchecked Exceptions with REST APIs

According to [1], "When deciding on checked exceptions vs. unchecked exceptions, ask yourself, What action can the client code take when the exception occurs?. If the Client code cannot do anything, Make it an unchecked exception. And if the…
Grainier
  • 1,634
  • 2
  • 17
  • 30
2
votes
2 answers

Should I use a relevant built-in unchecked exception where the theory prescribes using a checked one?

There are quite a few posts on SO about the "checked vs unchecked exception" topic. This answer is probably the most well-rounded and informative. Yet I'm still conflicted to follow the logic presented there, and there's a reason for that. I'm…
Semisonic
  • 1,152
  • 8
  • 21
2
votes
0 answers

checked Exception handling in java

In the following piece of code : import java.io.*; import java.io.FileReader; public class ExceptionPropagationDemo { public static void main(String[] args){ ExceptionPropagationDemo testObject =new ExceptionPropagationDemo(); …
shirin
  • 152
  • 1
  • 14
2
votes
1 answer

JUnit handling of RuntimeException (specifically)

I tend to throw as many checked Exceptions up as possible: it declutters the code (and I regard checked Exceptions as a dubious aspect of Java). I tend to use them when "refining" code.. i.e. when it makes sense for the particular context. This…
mike rodent
  • 14,126
  • 11
  • 103
  • 157
2
votes
1 answer

Durian's Errors class rethrow().wrap method invokes compiler error

I am trying to use Errors class of Durian library, for catching checked exceptions in lambda functions of Java 8. I wrote simple function: void eat(NsiItemInfoType food) throws CantUpdNSI {} And trying to call it: this code works well: …
Leo
  • 1,029
  • 4
  • 19
  • 40
2
votes
2 answers

Why is this considered an unhandled exception?

The following code doesn't compile because of an unhandled exception, though it seems to me like there should be no problem: class Car { public void drive() throws Exception { System.out.println("Driving..."); } } public class Sedan…
John Thompson
  • 1,674
  • 1
  • 20
  • 35
2
votes
3 answers

Throwing exceptions outside of a method - Java

I am a beginner in Java. I declared a method as public void method() throws Exception, but whenever I try to call that method in another area of the same class by using method();, I get an error: Error: unreported exception java.lang.Exception; must…
Sam Mitchell
  • 194
  • 2
  • 18
2
votes
1 answer

Not clear about the "Checked exception" explanation in "functional programming in Scala"

In the book of "Functional programming in Scala", there are some words talk about the "checked exception": Checked exceptions Java’s checked exceptions at least force a decision about whether to handle or reraise an error, but they result in…
Freewind
  • 193,756
  • 157
  • 432
  • 708
2
votes
3 answers

Interceptors Java and checked exception

I am using Spring's AOP feature. I have class called class UserService { public User insertUserService(User user) throws PersistenceLayerException { System.out.println("UserServiceImpl_Two called: insertUserService method"); if…
TimeToCodeTheRoad
  • 7,032
  • 16
  • 57
  • 70
2
votes
1 answer

How does that exceprt from the JDK get compiled?

The file is java.nio.channels.SocketChannel.java. JDK 7u45. The excerpt is: public static SocketChannel open(SocketAddress remote) throws IOException { SocketChannel sc = open(); try { sc.connect(remote); } catch (Throwable…
danissimo
  • 327
  • 3
  • 10
1
vote
0 answers

Why doesn't Java treat explicit division by zero as a compile-time exception?

Why is explicit division By zero not considered as compile time exception in JAVA ? Example : int x =9; int y = (int)x/0; Shouldnt compiler notify me about it ? I tried running the same and finding answers to it , I am not convinced that why is that…
1
vote
1 answer

Can I avoid illgealAccessException when checking if Class private fields are null or empty?

This is the block of code inside the boolean method that I want to check if there are any empty or null fields. It returns true if none of them are. If they are, it throws a custom exception for each case, but it messes up my entire program when it…
nikosidij
  • 21
  • 6
1
vote
0 answers

Propagating checked exceptions for an AutoClosable wrapper

Is there a way to declare a method in a generic type that wraps another method for another type and declares the same thrown exceptions? The most common example I can think of for this would be AutoClosable. public class LazyReference
BrainStorm.exe
  • 1,565
  • 3
  • 23
  • 40
1
vote
0 answers

Throw Exception for Invalid JSONPath while parsing json for specific object key

I am trying to read a list from a specific object key cars Input is jsonString: { "id": 600000, "name": "Ayman", "cars": [ { "manufacturer": "Volvo", "economy" : 65, "price" : 100000 } } Desired output is: "cars": [ …
Ayman Arif
  • 1,456
  • 3
  • 16
  • 40