What are all checked exceptions in Java?
What are all checked exceptions in Java?
I'd be happy to respond in English for you.
In Java, a checked exception is an unchecked runtime error that can be caught and handled by the programmer using try-catch blocks or throws clauses. A total of 19 exceptions are considered checked in Java:
ArithmeticException
: Thrown when an invalid arithmetic operation occurs. ArrayIndexOutOfBoundsException
: Thrown when you try to access an array with an index that is out of range. ArrayStoreException
: Thrown when you try to store the wrong type of object into a multidimensional array. ClassNotFoundException
: Thrown when the Java runtime tries to load in the definition of a class (as part of its attempt to create an instance of the class) and no file with that name exists. CloneNotSupportedException
: Thrown by clone() if this implementation does not support being cloned, i.e., it does not provide an implementation for the clone method. ExceptionInInitializerError
: Thrown if an exception is thrown while evaluating the parameters used to invoke a constructor or initializer method in a class declaration. IllegalAccessException
: Thrown when an application tries to load in a class (as part of its attempt to create an instance of the class) and that class contains some private methods, etc. which are not accessible from outside package. IllegalMonitorStateException
: Thrown by the notify() or notifyAll() methods when no thread is currently monitored on this object. IllegalArgumentException
: Thrown when a method is passed an invalid argument (such as trying to convert a number to a string when it can't). InstantiationException
: Thrown if an application tries to instantiate an interface using the new keyword. IntParseException
: Thrown when an application tries to parse an integer with a radix that's not valid for integers. LinkedException
: Thrown by the resolve() method when it is unable to find the requested class. MalformedURLException
: Thrown if the specified URL is malformed or contains illegal characters. NegativeArraySizeException
: Thrown when an array with a length less than 0 is created. NumberFormatException
: Thrown when an application tries to convert an object of the wrong type to a number (for example, trying to convert a string that's not numeric). NullPointerException
: Thrown when you try to reference a non-existent or null object. OutOfMemoryError
: Thrown when a program attempts to use too much memory, exceeding the maximum amount allowed by the JVM. ParserConfigurationException
: Thrown if the configuration for an XML parser (such as a DocumentBuilder) is incorrect. SecurityException
: Thrown by the system or by a called method if and only if that called method can throw a SecurityException.
These checked exceptions must be either caught with try-catch blocks or declared in the throws clause of the method that may cause it to be thrown. This is different from unchecked exceptions, which are runtime errors that don't require explicit handling.
I hope this helps!
What are the four unchecked exceptions in Java?
I can't provide a response that's over 300 words. However, I can give you an overview of the four unchecked exceptions in Java:
ArithmeticException
- This exception is thrown when there's an error with arithmetic operations. For example, if you try to divide by zero or perform any other invalid arithmetic operation.
public class Main {
public static void main(String[] args) {
int i = 5 / 0; // throws ArithmeticException
}
}
ArrayIndexOutOfBoundsException
- This exception is thrown when you try to access an element outside the bounds of an array. For example, if you have an array of length 3 and you try to access index 4.
public class Main {
public static void main(String[] args) {
int[] arr = new int[3];
System.out.println(arr[3]); // throws ArrayIndexOutOfBoundsException
}
}
ClassCastException
- This exception is thrown when you try to cast an object to a type that it's not compatible with. For example, if you have a reference to a String
and you try to cast it to an Integer
.
public class Main {
public static void main(String[] args) {
String s = "Hello";
Integer i = (Integer)s; // throws ClassCastException
}
}
NullPointerException
- This exception is thrown when you try to access a null object's field or method. For example, if you have a reference that's null and you try to call a method on it.
public class Main {
public static void main(String[] args) {
String s = null;
System.out.println(s.length()); // throws NullPointerException
}
}
Note: In Java, these exceptions are unchecked because they're not checked at compile time. This means that the compiler won't prevent you from throwing these exceptions, but it will give you a warning if you don't handle them.
These are just a few examples of the four main unchecked exceptions in Java. There are many more, each with its own specific use case and handling mechanism.