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
3
votes
3 answers

Is there any other role of throws instead of propagating a checked Exception?

After studying more and more about throws statement in Exception Handling I am getting confused.I found- If a method is capable of causing an exception that it does not handle, it must specify this behavior so that callers of the method can…
TheCurious
  • 593
  • 1
  • 4
  • 29
3
votes
2 answers

Always Use Checked Exceptions

I have been re-factoring some Java code lately... I have found that there were many RuntimeExceptions being thrown (i.e. unchecked exceptions). I have created my own checked exception and replaced each instance of these RuntimeExceptions with my own…
Chris Bolton
  • 2,162
  • 4
  • 36
  • 75
3
votes
2 answers

Declaring the same checked exception multiple times

I have just realized that I can write a method declaring the same checked exception several times. public void myMethod() throws MyException, MyException, MyException { I can't think of a reason why I would want to do this. I have been searching…
3
votes
4 answers

Are multiply-thrown Exceptions checked or runtime?

I have an Exception chain in which method1 throws an Exception to method2 which throws the Exception on to main. For some reason, the compiler forces me to deal with the error in method2 and marks it as an error if I don't, indicating that it's a…
froadie
  • 79,995
  • 75
  • 166
  • 235
3
votes
2 answers

How to convert / wrap Unchecked Exceptions into Checked Exceptions in Java?

Can Unchecked Exceptions be converted into Checked Exceptions in Java? If yes, please suggest ways to convert/wrap an Unchecked Exception into a Checked Exception.
3
votes
5 answers

How to decide whether I should add an exception to the method signature or handle it in the method?

I have quite a lot of experience with java (math, UIs and graphics mostly), but I've never seriously worked with APIs like JDBC or org.w3c.dom where you are heavily relied on handling checked runtime exceptions. So if I write a bunch of methods…
gvlasov
  • 18,638
  • 21
  • 74
  • 110
3
votes
3 answers

Checked or Unchecked Exception

Possible Duplicate: When to choose checked and unchecked exceptions Hello! So, I'm still getting comfortable regarding when to throw a checked or unchecked exception. I would like to know what others think is the most appropriate in this…
Scott
  • 257
  • 2
  • 3
  • 8
2
votes
3 answers

Java exceptions and inner exceptions, am I doing this correctly?

I'm validating a xml against a xsd, and I'm finding that there are many cases where exceptions have to be handled. I believe these are called checked exceptions in java? SchemaFactory sf = .... Schema schema = sf.newSchema(...) // SAXException…
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
2
votes
0 answers

Why can a caught Throwable be rethrown without declaring Throwable in the method's throws clause?

JLS 11.2.3 says that it is a compile time error if a method body can throw a checked exception and it does not declare the class (or a parent class) in its throws clause. In the following code, Throwable is a checked exception (per JLS 11.1.1) being…
MattDs17
  • 401
  • 1
  • 4
  • 20
2
votes
0 answers

Avoiding a checked exception in a constructor

Problem I have a class with two constructors. One of them (A) throws a checked exception, and the other one (B) calls the first one. // Constructor A public ArrayValue(DataType rules, Value... content) throws NonExpressionException { …
xtay2
  • 453
  • 3
  • 14
2
votes
0 answers

Any downsides to rethrowing the *cause* of a checked exception as an unchecked exception?

I am writing a library in which I use checked exceptions internally as it helps to make sure that all paths handle certain exceptional cases. However, I don't want to burden the users of my library with these checked exceptions because of this rule…
john16384
  • 7,800
  • 2
  • 30
  • 44
2
votes
2 answers

Why is it said: "Checked exceptions are not forwarded in calling chain (propagated)"?

For unchecked (runtime) exceptions it is understandable. They are literally propagated through stack. But why is it said that checked exceptions are not? Yes, we have to declare (throws) or handle them (try-catch), but why is it not called…
Stefan
  • 969
  • 6
  • 9
2
votes
2 answers

How to know if my code is using a checked or an unchecked exception?

Are there any keywords or special syntax of code that if I look at it, I'll know that here unchecked exception is being used or a checked exception is being used? From reading previous posts that asked this question before, it seems to me that the…
Viola
  • 33
  • 1
  • 1
  • 8
2
votes
2 answers

How does elm's compilation differ from Java's checked exceptions?

elm's claim of zero-runtime-exceptions is one of its major selling point (see official website), But if you stop to think about it, nothing stops you from dividing by zero or running out of memory. What the elm compiler basically does, is forcing…
Uri Goren
  • 13,386
  • 6
  • 58
  • 110
2
votes
1 answer

How does trick with rethrow checked exceptions works from the point of view of Java language?

I have encountered that I must catch all checked exceptions inside stream expresions. I have read very popular topic: How can I throw CHECKED exceptions from inside Java 8 streams? And there is answer which suugest following approach: we have 3…
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710