Answers for "checked and unchecked exceptions in java"

6

What are checked Exceptions

1) All the subclasses of Throwable class except error,Runtime Exception and 
its subclasses are checked exceptions.
2) Checked exception should be thrown with keyword throws or should be provided 
try catch block, else the program would not compile. We do get compilation 
error.
Examples :
1) IOException,
2) SQlException,
3) FileNotFoundException,
4) InvocationTargetException,
5) CloneNotSupportedException
6) ClassNotFoundException
7) InstantiationException
Posted by: Guest on December-01-2020
4

Unchecked exception

All subclasses of RuntimeException are called unchecked exceptions. 
These are unchecked exceptions because compiler does not checks if a method 
handles or throws exceptions. Program compiles even if we do not catch the 
exception or throws the exception. If an exception occurs in the program,
program terminates. It is difficult to handle these exceptions
because there may be many places causing exceptions.
Example : 
1) Arithmetic Exception
2) ArrayIndexOutOfBoundsException
3) ClassCastException
4) IndexOutOfBoundException
5) NullPointerException
6) NumberFormatException
7) StringIndexOutOfBounds
8) UnsupportedOperationException
Posted by: Guest on December-01-2020
0

Java Unchecked Exceptions

//pErrors and RuntimeExceptions are unchecked — that is, the compiler does not enforce (check) that you handle them explicitly. 
Methods do not have to declare that they throw them (in the method signatures). 
It is assumed that the application cannot do anything to recover from these exceptions (at runtime). 
Example
If you have declared an array of size 5 in your program, and trying to call the 6th element of the array then an ArrayIndexOutOfBoundsException occurs. 

public class Unchecked_Demo {
   
   public static void main(String args[]) {
      int num[] = {1, 2, 3, 4};
      System.out.println(num[5]);
   }
}
Errors
These are not exceptions at all, but problems that arise beyond the control of the user or the programmer. Errors are typically ignored in your code because you can rarely do anything about an error. For example, if a stack overflow occurs, an error will arise. They are also ignored at the time of compilation. 

Exception Hierarchy
 All exception classes are subtypes of the java.lang.Exception class. The exception class is a subclass of the Throwable class. Other than the exception class there is another subclass called Error which is derived from the Throwable class.
Posted by: Guest on August-31-2021

Code answers related to "checked and unchecked exceptions in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language