What is checked and unchecked exception in Java with example
Emma Valentine
Updated on March 29, 2026
The main difference between checked and unchecked exception is that the checked exceptions are checked at compile-time while unchecked exceptions are checked at runtime.
What is check and unchecked exception in Java?
The main difference between checked and unchecked exception is that the checked exceptions are checked at compile-time while unchecked exceptions are checked at runtime.
What are checked exceptions give an example?
Checked Exceptions In general, checked exceptions represent errors outside the control of the program. For example, the constructor of FileInputStream throws FileNotFoundException if the input file does not exist. Java verifies checked exceptions at compile-time.
What is checked and unchecked exception?
A checked exception must be handled either by re-throwing or with a try catch block, a runtime isn’t required to be handled. An unchecked exception is a programming error and are fatal, whereas a checked exception is an exception condition within your codes logic and can be recovered or retried from.Which of the following is example of checked exception in Java?
ClassNotFoundException, IOException, SQLException etc are the examples of the checked exceptions.
Why FileNotFoundException is checked exception?
FileNotFoundException is a checked exception in Java. Anytime, we want to read a file from the filesystem, Java forces us to handle an error situation where the file may not be present in the place. In the above example, you will get compile-time error with the message – Unhandled exception type FileNotFoundException .
Is SQLException checked or unchecked?
The classes that directly inherit the Throwable class except RuntimeException and Error are known as checked exceptions. For example, IOException, SQLException, etc. Checked exceptions are checked at compile-time.
How are checked exceptions defined in Java?
The exceptions that are checked during the compile-time are termed as Checked exceptions in Java. The Java compiler checks the checked exceptions during compilation to verify that a method that is throwing an exception contains the code to handle the exception with the try-catch block or not.How do you write a checked exception?
If you want to write a checked exception that is automatically enforced by the Handle or Declare Rule, you need to extend the Exception class. If you want to write a runtime exception, you need to extend the RuntimeException class.
Is throwable a checked exception?All other throwables — Throwable , Exception , and all of their subclasses except for those of the RuntimeException and Error lineage — are checked exceptions. The compiler requires these exceptions to be caught or declared when it’s possible for them to be thrown.
Article first time published onWhich of the below are unchecked exceptions in Java?
In Java exceptions under Error and RuntimeException classes are unchecked exceptions, everything else under throwable is checked. Consider the following Java program. It compiles fine, but it throws ArithmeticException when run. The compiler allows it to compile because ArithmeticException is an unchecked exception.
Is null pointer exception is an example of unchecked exception?
NullPointerException is an unchecked exception and extends RuntimeException class. Hence there is no compulsion for the programmer to catch it.
Can we catch unchecked exception in Java?
Yes you can handle the unchecked exception but not compulsory.
Why checked exceptions are not propagated in Java?
1 Answer. Because you haven’t declared them in the method declaration. Checked exceptions must either be handled in the method where they can occur, or they must be declared to be “thrown” by that method.
Is ArrayIndexOutOfBoundsException checked or unchecked?
ArrayIndexOutofBoundsException is an unchecked exception. Therefore, the java compiler will not check if a given block of code throws it.
Is FileNotFound unchecked?
I know FileNotFound is Checked Exception but though it is, only during the Run time this exception will occur.It is more like Arithmetic Exception(Unchecked). Whether it is checked or unchecked the exception will happen only during runtime.
Is FileNotFound a runtime error?
io. FileNotFoundException occurs at runtime so it is a checked exception, we can handle this exception by java code, and we have to take care of the code so that this exception doesn’t occur. …
Is the superclass of all unchecked exception?
The Throwable class is the superclass of all Java exceptions and errors. … The Exception class has a subclass called RuntimeException that contains most unchecked exceptions. All other subclasses of Exception are handled as checked exceptions.
Which of the following is not a checked exception?
Explanation: ArithmeticException is an unchecked exception, i.e., not checked by the compiler.
What is checked exception?
A checked exception is a type of exception that must be either caught or declared in the method in which it is thrown. For example, the java.io.IOException is a checked exception.
Why NULL pointer is a unchecked exception?
NullPointerException is a RuntimeException. In Java, a special null value can be assigned to an object reference. NullPointerException is thrown when program attempts to use an object reference that has the null value.
What is objects RequireNonNull?
RequireNonNull(Object, ISupplier) Checks that the specified object reference is not null . C# Copy. [Android.Runtime.Register(“requireNonNull”, “(Ljava/lang/Object;Ljava/util/function/Supplier;)Ljava/lang/Object;”, “”, ApiSince=24)] [Java.Interop.JavaTypeParameters(new System.String[] { “T” })] public static Java.Lang.
Is error a checked exception?
Everything under throwable except ERROR and RuntimeException are checked exception. Adding Runtime exception in program will decrease the clarity of program.
How do you stop a checked exception in Java?
The Solution: Use Unchecked Exceptions And Wrap Checked Exceptions. If you define your own exception, always use unchecked exceptions (RuntimeException). If you have to handle a checked exception, wrap them in your own domain/high-level exception and rethrow them.
Can unchecked exceptions be propagated?
Note : By default, Unchecked Exceptions are forwarded in calling chain (propagated). Unlike Unchecked Exceptions, the propagation of exception does not happen in case of Checked Exception and its mandatory to use throw keyword here. Only unchecked exceptions are propagated.
Which exception propagated automatically in Java?
unchecked exceptions are automatically propagated in java. Now, i’ll be explaining you how unchecked exception was propagated.
What is funneling of exception in Java?
Exception propagation in Java occurs when an exception thrown from the top of the stack. When it is not caught, the exception drops down the call stack of the preceding method. … This continues until the method reaches the bottom of the call stack or is caught somewhere in between.