Data Types and Operators
(Non-Primitive Data Types)
Control Flow Statements
Conditional Statements
Looping Statements
Branching Statements
Object-Oriented Programming (OOP)
Exception Handling
Collections Framework
Overview of Collections
Java I/O
Multithreading
GUI Programming with Swing
Advanced Topics
JAVA CODE
Java Basics
Working with Objects
Arrays, Conditionals, and Loops
Creating Classes and Applications in Java
More About Methods
Java Applet Basics
Graphics, Fonts, and Color
Simple Animation and Threads
More Animation, Images, and Sound
Managing Simple Events and Interactivity
Creating User Interfaces with the awt
Windows, Networking, and Other Tidbits
Modifiers, Access Control, and Class Design
Packages and Interfaces
Exceptions
Multithreading
Streams and I/O
Using Native Methods and Libraries
Under the Hood
Java Programming Tools
Working with Data Structures in Java
Advanced Animation and Media
Fun with Image Filters
Client/Server Networking in Java
Emerging Technologies
appendix A :- Language Summary
appendix B :- Class Hierarchy Diagrams
appendix C The Java Class Library
appendix D Bytecodes Reference
appendix E java.applet Package Reference
appendix F java.awt Package Reference
appendix G java.awt.image Package Reference
appendix H java.awt.peer Package Reference
appendix I java.io Package Reference
appendix J java.lang Package Reference
appendix K java.net Package Reference
appendix L java.util Package Reference

In Java, exceptions are a way to handle errors and other exceptional events that occur during the execution of a program. Exceptions are objects that describe an exceptional condition that has occurred in a piece of code. Java provides a robust and object-oriented way to handle exceptions. Here is a detailed description of the various types of exceptions in Java:

1. Checked Exceptions

Checked exceptions are exceptions that are checked at compile-time. These exceptions are typically beyond the control of the program and should be handled by the programmer. They are subclasses of the ‘Exception‘ class.

Examples of Checked Exceptions:

  • ClassNotFoundException: This exception is thrown when the application tries to load a class through its string name but no definition for the class with the specified name could be found.
  • IOException: This is a general class of exceptions produced by failed or interrupted I/O operations.
  • SQLException: This exception provides information on a database access error or other errors.
  • FileNotFoundException: This exception is thrown when an attempt to open the file denoted by a specified pathname has failed.

Handling Checked Exceptions:

				
					try {
    // Code that might throw a checked exception
} catch (IOException e) {
    // Handling code
}

				
			

2. Unchecked Exceptions

Unchecked exceptions are not checked at compile-time but rather at runtime. They are subclasses of the ‘RuntimeException‘ class. These exceptions are typically due to programming bugs, such as logic errors or improper use of an API.

Examples of Unchecked Exceptions:

  • NullPointerException: This exception is thrown when an application attempts to use null where an object is required.
  • ArrayIndexOutOfBoundsException: This exception is thrown to indicate that an array has been accessed with an illegal index.
  • ArithmeticException: This exception is thrown when an exceptional arithmetic condition has occurred, such as division by zero.
  • ClassCastException: This exception is thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance.

Handling Unchecked Exceptions:

				
					try {
    // Code that might throw an unchecked exception
} catch (NullPointerException e) {
    // Handling code
}

				
			

3. Errors

Errors are not exceptions but serious problems that are not intended to be caught by a program. They are usually external to the application and indicate serious problems that a reasonable application should not try to catch. Errors are subclasses of the ‘Error‘ class.

Examples of Errors:

  • OutOfMemoryError: This error is thrown when the Java Virtual Machine cannot allocate an object because it is out of memory.
  • StackOverflowError: This error is thrown when a stack overflow occurs because an application recurses too deeply.
  • VirtualMachineError: This error is thrown to indicate that the Java Virtual Machine is broken or has run out of resources necessary for it to continue operating.

Handling Errors:

Generally, you should not catch errors in your application. However, you can handle them in rare cases where you need to clean up resources or log the error before terminating.

				
					try {
    // Code that might cause an error
} catch (Error e) {
    // Handling code
}

				
			

4. Custom Exceptions

Java allows you to create your own exception classes. These exceptions can be checked or unchecked, depending on the superclass you choose.

Creating a Custom Checked Exception:

				
					public class MyCheckedException extends Exception {
    public MyCheckedException(String message) {
        super(message);
    }
}

				
			

Creating a Custom Unchecked Exception:

				
					public class MyUncheckedException extends RuntimeException {
    public MyUncheckedException(String message) {
        super(message);
    }
}

				
			
  • Checked Exceptions: Must be caught or declared in the method signature using throws. Examples include IOException, SQLException.
  • Unchecked Exceptions: Do not need to be caught or declared. Examples include NullPointerException, ArrayIndexOutOfBoundsException.
  • Errors: Serious problems not intended to be caught by applications. Examples include OutOfMemoryError, StackOverflowError.
  • Custom Exceptions: You can define your own exceptions based on your application needs.

Handling exceptions properly is crucial for building robust and fault-tolerant applications.

Scroll to Top