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
757
votes
21 answers

Understanding checked vs unchecked exceptions in Java

Joshua Bloch in "Effective Java" said that Use checked exceptions for recoverable conditions and runtime exceptions for programming errors (Item 58 in 2nd edition) Let's see if I understand this correctly. Here is my understanding of a…
528
votes
33 answers

The case against checked exceptions

For a number of years now I have been unable to get a decent answer to the following question: why are some developers so against checked exceptions? I have had numerous conversations, read things on blogs, read what Bruce Eckel had to say (the…
TofuBeer
  • 60,850
  • 18
  • 118
  • 163
365
votes
18 answers

How can I throw CHECKED exceptions from inside Java 8 lambdas/streams?

How can I throw CHECKED exceptions from inside Java 8 lambda, used in a stream for example? In other words, I want to make code like this compile: public List getClasses() throws ClassNotFoundException { List classes = …
Marcelo Glasberg
  • 29,013
  • 23
  • 109
  • 133
264
votes
18 answers

When to choose checked and unchecked exceptions

In Java (or any other language with checked exceptions), when creating your own exception class, how do you decide whether it should be checked or unchecked? My instinct is to say that a checked exception would be called for in cases where the…
Matt Sheppard
  • 116,545
  • 46
  • 111
  • 131
133
votes
8 answers

Why is "throws Exception" necessary when calling a function?

class throwseg1 { void show() throws Exception { throw new Exception("my.own.Exception"); } void show2() throws Exception // Why throws is necessary here ? { show(); } void show3() throws Exception //…
nr5
  • 4,228
  • 8
  • 42
  • 82
71
votes
9 answers

Java 8: Mandatory checked exceptions handling in lambda expressions. Why mandatory, not optional?

I'm playing with the new lambda features in Java 8, and found that the practices offered by Java 8 are really useful. However, I'm wondering is there a good way to make a work-around for the following scenario. Suppose you have an object pool…
Lyubomyr Shaydariv
  • 20,327
  • 12
  • 64
  • 105
37
votes
13 answers

Declare a method that always throws an exception?

I have a method like: int f() { try { int i = process(); return i; } catch(Exception ex) { ThrowSpecificFault(ex); } } This produces a compiler error, "not all code paths return a value". But in my case ThrowSpecificFault() will…
noctonura
  • 12,763
  • 10
  • 52
  • 85
35
votes
3 answers

Why is catching checked exceptions allowed for code that does not throw exceptions?

In Java, methods that throw checked exceptions (Exception or its subtypes - IOException, InterruptedException, etc) must declare throws statement: public abstract int read() throws IOException; Methods that do not declare throws statement can't…
34
votes
4 answers

Wrapping a checked exception into an unchecked exception in Java?

I have this factory method in java: public static Properties getConfigFactory() throws ClassNotFoundException, IOException { if (config == null) { InputStream in = Class.forName(PACKAGE_NAME).getResourceAsStream(CONFIG_PROP); …
James McMahon
  • 48,506
  • 64
  • 207
  • 283
33
votes
8 answers

How to wrap checked exceptions but keep the original runtime exceptions in Java

I have some code that might throw both checked and runtime exceptions. I'd like to catch the checked exception and wrap it with a runtime exception. But if a RuntimeException is thrown, I don't have to wrap it as it's already a runtime…
AlikElzin-kilaka
  • 34,335
  • 35
  • 194
  • 277
29
votes
5 answers

What parts of the JLS justify being able to throw checked exceptions as if they were unchecked?

I have recently discovered and blogged about the fact that it is possible to sneak a checked exception through the javac compiler and throw it where it mustn't be thrown. This compiles and runs in Java 6 and 7, throwing a SQLException without throws…
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
28
votes
6 answers

Application of @Sneaky Throws in lombok

I was playing with the Lombok library in Java and found an annotation called @SneakyThrows. As the documentation states: @SneakyThrows fakes out the compiler. In other words, Lombok doesn't wrap or replace the thrown checked exception, but makes…
26
votes
8 answers

Differences between Runtime/Checked/Unchecked/Error/Exception

What are the Runtime exceptions and what are Checked/Unchecked Exceptions and difference between Error/Exception.Why these many types? Instead Java may simply follow a simple design(just try/catch all types) to handle an abnormal condition in a…
JavaUser
  • 25,542
  • 46
  • 113
  • 139
24
votes
2 answers

Guava cache and preserving checked exceptions

I'm refactoring some code to use guava Cache. Initial code: public Post getPost(Integer key) throws SQLException, IOException { return PostsDB.findPostByID(key); } In order not to break something I need to preserve any thrown exception as is,…
Vadzim
  • 24,954
  • 11
  • 143
  • 151
24
votes
2 answers

How do I fix a compilation error for unhandled exception on call to Thread.sleep()?

I am new to Java and kind of new to programming (I know diving straight into Java probably wasn't the greatest idea.) and I've been getting an error consistently no matter how I try to add a pause in my program. I am doing a simple counting program…
Mr.Crippled
  • 287
  • 1
  • 2
  • 6
1
2 3
9 10