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
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…
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…
35
votes
9 answers

Difference between Unchecked exception or runtime exception

This was an interview question. What is the main difference between unchecked exception and error as both are not caught? They will terminate the program.
giri
  • 26,773
  • 63
  • 143
  • 176
23
votes
6 answers

How to identify checked and unchecked exceptions in java?

While reading about exception, I will always come across checked exceptions and unchecked exceptions, So wanted to know how to distinguish that which is what? Edit: I want to know if i create any exception class then how can i create as a checked or…
GuruKulki
  • 25,776
  • 50
  • 140
  • 201
20
votes
2 answers

Is there any easy way to see what exceptions a Kotlin function throws?

I mostly understand the potential issues with checked exceptions and why Kotlin omits them. However, the issue I am encountering is I can't find any foolproof way of clearly indicating to the caller what exceptions a function may throw. I have run…
zjuhasz
  • 1,489
  • 12
  • 30
14
votes
3 answers

Is there an advantage to declaring that a method throws an unchecked exception?

If I have a method which throws an unchecked exception, e.g.: void doSomething(int i) { if (i < 0) throw new IllegalArgumentException("Too small"); // ... } is there any advantage to explicitly declaring that the method throws the exception,…
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
10
votes
4 answers

Hard time understanting checked & unchecked exceptions

I've already read everything I could about this and I still don't understand how to use checked and unchecked exceptions. I think I can't still grasp the concept. I've read around StackOverflow that it's better to use unchecked rather than checked…
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
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
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
8 answers

Java - checked vs unchecked Exception - tell from code alone?

Is it possible to tell if an exception class is a checked or unchecked just by looking at the code? I always thought that if it extended Exception, it was checked, but then RuntimeException extends Exception and that is unchecked. RuntimeException…
Michael Ford
  • 851
  • 6
  • 9
5
votes
2 answers

Should I declare an unchecked exception?

I've got a method which invokes another method like this: public void m1() { m2(10); } public void m2(int v) { if(v < 10) throw new MyException(); } public class MyException extends RuntimeException{ } Now, I'd like to notify…
Alupkers
  • 223
  • 1
  • 9
5
votes
2 answers

How does JVM handles RuntimeException(s)

While creating custom exceptions, If we want to create a checked Exception we extend the Exception class and for unchecked exception we extend the RuntimeException class. My question is, how JVM handles subClasses of RuntimeException and Exception…
gaurs
  • 575
  • 1
  • 5
  • 19
4
votes
2 answers

Java 8 lambda catching exception

From the book Java 8 for the impatient by Cay Horstmann: Didn’t you always hate it that you had to deal with checked exceptions in a Runnable? Write a method uncheck that catches all checked exceptions and turns them into unchecked exceptions.…
user1539343
  • 1,569
  • 6
  • 28
  • 45
4
votes
1 answer

What is a good practice of dealing with some runtime HTTP exceptions?

I have a small method that looks like this: public static void unstarTrack(Context ctxContext, String strId) { try { HttpParams htpParameters = new BasicHttpParams(); List lstCredentials = new…
Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382
1
2 3 4 5