How many types of exceptions in java with examples
How many types of exceptions in java with examples
I'll be happy to help you with that!
There are several types of exceptions in Java, and I'll cover the most common ones along with examples. Keep in mind that these are just a few of the many exception types available.
1. Runtime Exceptions (Unchecked Exceptions)
These are runtime errors that occur during program execution. The compiler doesn't check for them explicitly. Examples include:
ArithmeticException
: Thrown when an arithmetic operation is invalid, like dividing by zero.
Example: int x = 5 / 0; // throws ArithmeticException
ArrayIndexOutOfBoundsException
: Thrown when trying to access an array element that is out of bounds.
Example: String[] arr = new String[3]; System.out.println(arr[3]); // throws ArrayIndexOutOfBoundsException
ClassCastException
: Thrown when trying to cast an object to a wrong type.
Example: Object obj = "hello"; String str = (String) obj; // throws ClassCastException
2. Checked Exceptions
These are errors that the compiler checks for, and you must explicitly handle them using try-catch blocks or propagate them up the call stack.
IOException
: Thrown when an I/O operation fails.
Example: File file = new File("non-existent-file.txt"); FileReader reader = new FileReader(file); // throws FileNotFoundException
SQLException
: Thrown when a SQL operation fails.
Example: Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/mydb", "user", "pass"); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM non-existent-table"); // throws SQLException
3. Logic Exceptions (Program Errors)
These are errors that occur due to logical mistakes in your code.
NullPointerException
: Thrown when trying to access a null object.
Example: String str = null; System.out.println(str.length()); // throws NullPointerException
IllegalStateException
: Thrown when an operation is illegal or impossible to perform.
Example: int x = 5; if (x < 0) throw new IllegalStateException();
4. Error Exceptions
These are errors that indicate a serious problem, like running out of resources.
OutOfMemoryError
: Thrown when the JVM can't allocate memory for your program.
Example: byte[] buffer = new byte[1024 * 1024 * 10]; // throws OutOfMemoryError
StackOverflowError
: Thrown when your method call stack exceeds a certain limit.
5. Other Exceptions
There are many more exceptions in Java, such as:
InterruptedException
: Thrown when a thread is interrupted. UnsupportedOperationException
: Thrown when an operation is not supported by a class or interface. UnknownHostException
: Thrown when a hostname lookup fails.
That's a brief overview of the most common types of exceptions in Java. Remember that each exception has its own unique characteristics, and it's crucial to handle them properly to ensure your program remains stable and robust!
How many types of exceptions in java javatpoint
I'd be happy to help you with that!
According to Javatpoint, an online learning platform for Java programming, there are several types of exceptions in Java. Here are some of the key ones:
Checked Exception: These are exceptions that must be explicitly handled by acatch
block or propagated through method calls. Examples include IOException
, SQLException
, and FileNotFoundException
. Unchecked Exception: Also known as "runtime exceptions," these are exceptions that can occur at runtime, such as ArithmeticException
, ArrayIndexOutOfBoundsException
, and ClassCastException
. These exceptions do not need to be explicitly handled by a catch
block. Error: Errors are severe programming errors, such as OutOfMemoryError
, StackOverflowError
, and LinkageError
. Unlike runtime exceptions, these cannot be caught or propagated through method calls. RuntimeException: This is an abstract class that is the superclass of all unchecked exceptions (runtime exceptions). Examples include ArrayIndexOutOfBoundsException
and ClassCastException
.
Exception: This is an abstract class that is the superclass of all checked exceptions. Examples include IOException
, SQLException
, and FileNotFoundException
. All Exception
subclasses are either runtime exceptions or errors. IOException: A subclass of Exception
, this represents exceptions that occur during input/output operations, such as file I/O, network I/O, or database I/O. SecurityException: A subclass of Exception
, this represents security-related exceptions that can occur when a program attempts to perform an unauthorized operation. ExceptionInInitializerError: A subclass of RuntimeException
, this represents an exception that occurs during the initialization of an object or class. LinkageError: A subclass of Error
, this represents errors that occur while linking classes together, such as loading a class that is not found or contains incorrect bytecode. VerifyError: A subclass of Error
, this represents errors that occur when verifying the correctness of bytecode, such as attempting to access an inaccessible field or method.
In summary, Java has several types of exceptions, including checked and unchecked exceptions, runtime exceptions, and errors. Understanding these different exception types is crucial for writing robust and reliable Java programs.
Would you like me to elaborate on any specific type of exception?