Questions tagged [unchecked-exception]

In Java programming, an exception that does not need to be declared in a method's throws clause, and is not required to be caught.

RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.

RuntimeException and its subclasses are unchecked exceptions. Unchecked exceptions do not 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/RuntimeException.html

63 questions
4
votes
3 answers

Unchecked exception when using static imports, how come?

I'm experiencing some strange behavior when using static imports of inherited static methods: com/example/util/BaseUtil.java: package com.example.util; /*default*/ class BaseUtil { public static final void foo(){ System.out.println("foo");…
Andrey
  • 8,882
  • 10
  • 58
  • 82
4
votes
6 answers

Should unchecked exceptions be caught and dealt with?

I have been reading many posts about exceptions lately and I have a question whether unchecked exceptions should be caught. I have read that if you want your application to recover from an error you use checked exceptions. However if you can't deal…
LuckyLuke
  • 47,771
  • 85
  • 270
  • 434
3
votes
4 answers

How to write an unchecked Throwable in java

I want to write a custom Throwable that is unchecked. There are ways to trick the compiler at throw time (e.g. Utility class that re-throws a throwable as unchecked?), which I have implemented thus: public class CustomThrowable extends Throwable { …
Gordon Bean
  • 4,272
  • 1
  • 32
  • 47
3
votes
2 answers

Unchecked cast behavior with and without assignment to variable

Why first line in main does not throw ClassCastException while the second does? import java.util.function.Function; class Scratch { static T getSomething(Function fun) { return (T) fun; } public static void…
Julian Rubin
  • 1,175
  • 1
  • 11
  • 23
3
votes
2 answers

How do I test expected Unchecked Exceptions in Kotlin?

I am trying to write a test in Kotlin that makes sure an unchecked exception is thrown in specific circumstances. I am trying to use org.junit.jupiter.api.Assertions.assertThrows like this: assertThrows(MyRuntimeException::class, Executable {…
Marcus Lanvers
  • 383
  • 5
  • 20
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

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.
2
votes
1 answer

Should @Transactional method roll back in case of calling a private method which throws RuntimeException in Spring?

Should transaction rolled back here? @Service public class Serv { @Transactional public void method1() { method2(); } private void method2(){ throw new RuntimeException(); } } Or does it depend on something? (…
J.J. Beam
  • 2,612
  • 2
  • 26
  • 55
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
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
3 answers

Java unchecked conversion of arraylist

Can anybody tell me how to create an array-arraylist the right way ?! .\File.java:5: warning: [unchecked] unchecked conversion ArrayList myParkingLotArray[] = new ArrayList[3]; …
2
votes
2 answers

Java Unchecked Exception

Can anyone tell me why this method is throwing an unchecked or unsafe exception? I understood that to happen when I try to edit the list I am iterating through... in the below, currentAdvanceTactics etc are not being iterated through at all. …
Peter F
  • 435
  • 1
  • 8
  • 17
1
vote
3 answers

Why does throwing an unchecked exception removes the "missing return statement" error

I am very new at programming and the problem thus might seem very silly. The below mentioned method has a return type as an int array. When we don't throw any unchecked exception it throws an error which I understand. But why does including an…
the_novice
  • 105
  • 7
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
1
vote
2 answers

Java method contains another method that throws exception

I have public method add(String) that calls private method inspectSequence(String) to check whether String valid or not. This method returns array if passed String is valid, if it's invalid then method throws IllegalArgumentException the code is…
Mike Herasimov
  • 1,319
  • 3
  • 14
  • 31