150 questions
3
votes
1
answer
40
views
Nodriver does not take exception if element not found?
I am trying to search for elements on a webpage and have used various methods, including text and XPath. It seems that the timeout option does not work the way I expected, and no exception is raised ...
1
vote
1
answer
109
views
What does AWS mean when they say that there are scalability issues with Checked Exceptions? [closed]
I am upgrading AWS' Java SDK from 1.x to 2.x. In 2.x, it seems that most exceptional cases are modeled with Unchecked Exceptions, as opposed to Checked Exceptions like it was in 1.x.
When trying to ...
9
votes
2
answers
587
views
Are there any differences between Checked and Unchecked Exceptions at Runtime?
Checked Exceptions are powerful because they allow you to force the use-site to deal with an exceptional case. If the use-site does not handle an exceptional case (or publically announce that they are ...
4
votes
1
answer
95
views
Compilation error when calling generic method which throws generic exception
I am compiling this class:
public class Test {
// No throws clause here
public static void main(String[] args) {
doThrow(new SQLException());
}
static void doThrow(Exception ...
1
vote
0
answers
163
views
exception for callable in phpstan
how can I describe the exceptions thrown from callable in phpDoc?
For example I have:
/**
* @param callable(int): bool
*/
function (callable $foo, int $number): ?bool
{
try {
return $foo(...
-1
votes
1
answer
222
views
Final rethrow in Java: Exception Handling
I was going through some Exception Handling concepts in Java and came across the concept of final rethrow. I read the following article-
https://baptiste-wicht.com/posts/2010/05/better-exception-...
0
votes
0
answers
23
views
Method reference of method throwing checked exception triggers compilation error at point method reference is named [duplicate]
I have a bunch of methods, with similar signatures, compatible with Function, they each declare throws InterruptedException, and I put them in a list and select one by its index and call its apply().
...
1
vote
0
answers
44
views
Why doesn't Java treat explicit division by zero as a compile-time exception? [duplicate]
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 ...
2
votes
0
answers
49
views
Why can a caught Throwable be rethrown without declaring Throwable in the method's throws clause? [duplicate]
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, ...
0
votes
2
answers
155
views
Is there a way to detect the overflow-checking context in code?
I'm working on an importer for LitJson, to import float values from ints, and doubles, and if overflow-checking is enabled, I want to wrap a potential overflow exception in a JsonException with a bit ...
0
votes
3
answers
80
views
Is it possible to pass on an exeption thrown during a stream operation?
Consider the following java code:
public void write(FrameConsumer fc) throws FFmpegFrameRecorder.Exception{
frameStream.forEach(n -> fc.consume(n));
}
In this case "...
0
votes
0
answers
34
views
Why Exceptions are categorized as Checked and unchecked? [duplicate]
I got one doubt regarding exception handling. We have checked and unchecked exceptions. Why they have given like that? irrespective checked or unchecked we have to handle the exception. Then i am ...
6
votes
1
answer
5k
views
What does "error: unreported exception <XXX>; 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 <XXX>; must be caught or declared to be thrown"
where XXX is the name of some ...
2
votes
1
answer
85
views
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)...
1
vote
1
answer
348
views
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 ...
-1
votes
3
answers
2k
views
Why we need to handle or throw checked exceptions in Java?
I am new to Java, I read that checked exception get raised during compile time only i.e. program will not compile successfully if there is a checked exception which is not handled or thrown.
If ...
2
votes
0
answers
97
views
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 ...
1
vote
1
answer
2k
views
Why doesn't Lombok.sneakyThrows throw a ClassCastException?
I am trying to understand how the @SneakyThrows annotation in Lombok actually works under the hood.
From this SO answer, I can gather it uses the method Lombok.sneakyThrows() under the hood.
The code ...
0
votes
1
answer
1k
views
Groovy Spock: How do you pass in new Exception into when method without throwing error right away
I am trying to test a kafka error handler that takes in an Exception but as soon as I declare it in spock it actually throws it.
def 'test example'() {
when:
service.emitError(new Exception('...
1
vote
0
answers
22
views
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 ...
0
votes
1
answer
88
views
Program complies even when we do not handle the checked Exception thrown by Frame constructor? [closed]
As per Oracle Documentation https://docs.oracle.com/javase/7/docs/api/java/awt/Frame.html#Frame()
Frame constructor throws HeadlessException - when GraphicsEnvironment.isHeadless() returns true
But ...
-1
votes
2
answers
70
views
Java Checked Exception
I'm struggling figuring out something about checked Exception in Java. What I've learned (I may be wrong) is that if you throw a checked Exception you have two options.
you have to catch it with try ...
0
votes
2
answers
182
views
Is it possible to throw an checked Exception implicitly, without any athrow instruction?
In Java, both checked exception and unchecked exception can be thrown explicitly, i.e, by a throw statement. Besides, unchecked exceptions like ArithmeticException and OutOfMemoryError can be ...
-1
votes
2
answers
263
views
Kotlin ignoring CheckedException leads to potential bug source
The following method will warn about IOException if called in Java code but will simply ignore any warning in Kotlin because IOException is a checked exception.
ParcelFileDescriptor.open(file, ...
7
votes
2
answers
712
views
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 ...
0
votes
1
answer
2k
views
React fetch error/response catching not working
I built a signup page that needs to check whether an inputted email is already taken.
When a duplicate email is taken, it gets stopped in the java spring api backend and returns a status of "400&...
38
votes
7
answers
59k
views
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 ...
2
votes
2
answers
968
views
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 ...
-4
votes
1
answer
953
views
Why can't Java Lambda throw an checked exception [closed]
I'm learning Java and am a bit confused, why Lambda Expressions can't throw checked exceptions. Anyone has an understandable explanation for this?
I read through this post:
Java 8 Lambda function that ...
1
vote
0
answers
49
views
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,
"...
33
votes
1
answer
9k
views
What's the idea behind Kotlin removing checked exceptions?
And how do we handle this when we are calling a method in Java which throws exceptions ?
my code is in kotlin and I am using a 3rd party library which is written in java. I call a method of this ...
25
votes
3
answers
57k
views
Getting Mockito Exception : checked exception is invalid for this method
I have a method which I am trying to test
public List<User> getUsers(String state) {
LOG.debug("Executing getUsers");
LOG.info("Fetching users from " + state);
List<...
0
votes
0
answers
104
views
How to Anticipate Exceptions in C# When There Are No Checked Exceptions
In Java, calls to functions that can throw Exceptions need to anticipate them with either a try-catch or by adding a throws Exception to their function signature.
My main use for them is in my top-...
22
votes
4
answers
5k
views
Why does the compiler allow throws when the method will never throw the Exception
I am wondering why the java compiler allows throws in the method declaration when the method will never throw the Exception. Because "throws" is a way of handling the exception (telling the caller to ...
0
votes
0
answers
25
views
What does it mean by propagating all exceptions in Java [duplicate]
I was told to propagate all exceptions. This sentence means active propagation such like :
public void doSomething() throws SomeException{
try{
doSomethingThatCanThrowException();
} catch (...
0
votes
1
answer
648
views
Getting Exception In onGuildMessageReceivedEvent - Discord JDA
I am making a Discord JDA bot that can when a user sends the message: Prefix("$") + hastebin + their code, the bot will create a request to hastebin and paste their code, after that he will take the ...
-3
votes
1
answer
3k
views
java ATM program simulation with exception handling - no error neither full output
the output is not a complete one, and neither are the exceptions handled.
Please help.
public class ATM {
private String message;
public ATM(String m) {
if (m == null || m.trim()....
2
votes
1
answer
3k
views
How to throw Checked Exception inside lambda Java8? [duplicate]
I am trying to throw an exception inside lambda but it keeps giving me an error saying that Unhandled IOException.
private <T> T getResponse(final RestURI query, final Class<T> ...
1
vote
0
answers
2k
views
How to throw a checked exception within CompletableFuture?
I used a static method supplyAsync() to get a CompletableFuture, and call its whenComplete() method to deal with the result and exception.
whenComplete() accepts a BiConsumer as parameter, and ...
0
votes
1
answer
48
views
Selective catch for a closeable resource
This is more of a syntax / structuring issue.
I'm working with JDBC: Statement and ResultSet, which means SQLExceptions are being thrown everywhere.
My code looks like this:
private static final ...
24
votes
3
answers
2k
views
Languages that use checked exceptions
I'm just curious, is there any other languages besides Java that uses checked exceptions ?
I did try to find information about this but couldn't find any answers.
3
votes
2
answers
1k
views
How can I collect results of a Java stream operation that throws exceptions in a concise manner?
Have a look at the following snippet that tries to convert a list of strings into a list of class objects:
public static List<Class<?>> f1(String... strings) {
return
Stream....
0
votes
3
answers
816
views
Comparing checked and unchecked exceptions (Performance, Benchmarks) in Java
Are there scenarios where you could argue that when an exception is created that both a checked exception and an unchecked exception can be used? Then the speed and performance can be measured against ...
2
votes
2
answers
1k
views
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 ...
2
votes
2
answers
235
views
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.
...
2
votes
1
answer
518
views
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 ...
1
vote
2
answers
2k
views
create checked exception class in java [duplicate]
I am puzzled about creating checked exception class in java.
Many article says custom exception can be created by
class MyException extends Exception
{
//constructor defined
}
Since ...
0
votes
1
answer
289
views
returning java.util.Optional or throw (Checked/Unchecked)exception
I need to create a method to find an employee by employee's name. There are three possible solution to implement this as below :
Employee findEmployeeById(long empId) throws ...
2
votes
1
answer
317
views
Why throws upcasting checked exception [duplicate]
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 ...
-2
votes
1
answer
103
views
Is Exception checked or not in JAVA? [duplicate]
Consider the following code
public void myMethod1() {
try {
this.getClass().getMethod("myMethod").invoke(this);
} catch (Exception e) {
throw e;
}
}
public void ...