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
8
votes
1 answer

Why one should try throw unchecked exception over checked exception?

I've been told that I should consider throwing Unchecked exception over Checked exception in my code and not only that, but to extend the RuntimeException with my own one. Now, I do understand the difference between the two, but still doesn't…
Nimrod
  • 1,100
  • 1
  • 11
  • 27
8
votes
3 answers

Throwing exception from lambda

Given this java 8 code public Server send(String message) { sessions.parallelStream() .map(Session::getBasicRemote) .forEach(basic -> { try { basic.sendText(message); } catch (IOException e) { …
vach
  • 10,571
  • 12
  • 68
  • 106
8
votes
2 answers

Custom Exception that wraps Multiple Exceptions : Encouraged or Not?

I am coding a Java Library that will be used to access a DB. I am throwing the exceptions to the end-programmer who uses the JAR library to handle it the way he/she wants. I wrote a custom Exception (provided below) to wrap connection specific…
ndnine89
  • 137
  • 2
  • 3
  • 12
8
votes
3 answers

Isn't an unchecked exception that is caught in a try block a checked exception in Java?

I was told that in Java, unchecked exceptions can be caught in a try block, but if it's caught, isn't it called a checked exception?
Anonymous
  • 4,133
  • 10
  • 31
  • 38
7
votes
2 answers

How do you judge whether to make an exception checked or unchecked?

I was reading about checked vs unchecked exceptions in Java and when to use each: Here's the bottom line: If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover…
temporary_user_name
  • 35,956
  • 47
  • 141
  • 220
7
votes
2 answers

List all exceptions that could be thrown by a method

I know that Java enforce the programmer to list all exceptions that will be thrown by the method, and thus creating an easy way of listing all possible thrown exception for the user of code. .NET on the other hand has no such feature, and all we're…
7
votes
4 answers

How would I know if I haven't handled some unchecked exceptions that my .NET code could throw?

In .NET, method signatures don't tell me if I have missed handling some exceptions that could be thrown by my code. Is there some tool that can warn me, if say I am using a HashTable remove but haven't handled the ArgumentNullException? I don't want…
Abhijeet Kashnia
  • 12,290
  • 8
  • 38
  • 50
7
votes
3 answers

Why are Runtime Exceptions "unchecked" in Java?

Why does it make sense to have Runtime Exceptions UnChecked (as opposed to if they were Checked)?
Moeb
  • 10,527
  • 31
  • 84
  • 110
7
votes
4 answers

dealing with catastrophic exceptions

I read in a C# introductory book that you shouldn't catch an exception if you don't know what to do with it. Thinking of that bit of advice while programming in Java, I sometimes find I do not know what to do with an exception, but I am forced to…
H2ONaCl
  • 10,644
  • 14
  • 70
  • 114
6
votes
2 answers

How to find exceptions that were "ignored"?

No need to declare checked exceptions in throws clause or handling them in try/catch block in scala is the feature that I love. But it can be a problem when exception must be handled but was ignored. I'm looking for tools (maybe compiler…
Jeriho
  • 7,129
  • 9
  • 41
  • 57
6
votes
2 answers

Is disabling Checked Exceptions in Java possible?

I was reading an article about checked and unchecked Exceptions in Java and found this article/link: https://projectlombok.org/disableCheckedExceptions.html According to the article it's just a hack developed for javac. Consider the code snippet…
NaveeNeo
  • 205
  • 3
  • 13
6
votes
2 answers

Checked exceptions thrown from within lambda expressions

Can you please explain why checked exceptions have to be caught from within lambda expressions? In other words, why does the following code not compile... public void doSomething(ObjectInputStream istream) throws IOException { // The read method…
BJ Dela Cruz
  • 5,194
  • 13
  • 51
  • 84
6
votes
1 answer

writing a JUnit Test case for custom checked exception in Java?

I am writing a test case for my class that has methods which throw exceptions (both checked and runtime). I have tried different possible ways of testing as suggested in this link.. It appears they seem to work only for runtime exceptions. for…
brain storm
  • 30,124
  • 69
  • 225
  • 393
5
votes
1 answer

What does "error: unreported exception ; must be caught or declared to be thrown" mean and how do I fix it?

New Java programmers frequently encounter errors phrased like this: "error: unreported exception ; must be caught or declared to be thrown" where XXX is the name of some exception class. Please explain: What the compilation error message is…
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
5
votes
1 answer

How does scala generated byte code drops the checked exception?

If it possible to write byte code for a method that is supposed to throw a checked exception? For instance the following Java class doesn't compile unless the method declares it throws the checked exception: public class CheckedExceptionJava { …
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
1 2
3
9 10