[!INDEX]
- Basic exception code ( 1 to 9 , between 100/0 )
- hierarchy
- Differences Between Errors and Exceptions
- Error code ( using recursive mt )
- Exception code ( filenotfound using BufferReader)
1. Basic exception code ( 1 to 9 , between 100/0 )
public class L02 {
public static void main(String[] args) {
System.out.println(1);
System.out.println(2);
System.out.println(3);
System.out.println(4);
System.out.println(100/0);
System.out.println(5);
System.out.println(6);
System.out.println(7);
System.out.println(8);
System.out.println(9);
}
}
2. hierarchy
Throwable
├── Error (not meant to be caught)
└── Exception
├── Checked Exceptions (e.g., IOException)
└── Unchecked Exceptions (e.g., NullPointerException)
Throwable
└── Error
├── LinkageError
│ ├── NoClassDefFoundError
│ ├── UnsatisfiedLinkError
│ └── ExceptionInInitializerError
├── VirtualMachineError
│ ├── OutOfMemoryError
│ ├── StackOverflowError
│ └── InternalError
├── AssertionError
├── ThreadDeath
├── UnknownError
└── ExternalError
└── Exception
├── IOException (and its subclasses)
│ ├── FileNotFoundException
│ ├── IntruptedIOException
│ └── EOFException
├── SQLException
├── ClassNotFoundException
└── RuntimeException
├── ArithmeticException
├── NullPointerException
├── IndexOutOfBoundsException
├── ArrayIndexOutOfBoundsException
├── StringIndexOutOfBoundsException
├── IllegalArgumentException
├── NumberFormatException
└── ClassCastException
└──
└── upto 1000
3. Differences Between Errors and Exceptions
[!NOTE]
- exception are occured by our program recoverable, unchecked and checked
- error are occured by system resource like ram , memory, cant recover , runtime
| Feature | Errors | Exceptions |
|---|---|---|
| Definition | Errors are serious problems that a reasonable application should not try to catch. They usually indicate issues that are beyond the control of the application, often related to the environment or the JVM itself. | Exceptions are conditions that a program can catch and handle. They indicate problems that can arise during the normal operation of the program. |
| Subclasses | Errors are subclasses of the Error class. Common examples include OutOfMemoryError, StackOverflowError, and InternalError. |
Exceptions are subclasses of the Exception class. They can be further divided into checked exceptions (e.g., IOException, SQLException) and unchecked exceptions (e.g., NullPointerException, ArithmeticException). |
| Handling | Errors are not meant to be caught or handled in a program. They usually indicate severe issues that cannot be recovered from. | Exceptions can be caught and handled using try-catch blocks. Developers can implement logic to manage these situations gracefully. |
| Recovery | Typically, there is no recovery from an error. Once an error occurs, the application may crash or terminate. | Exceptions can often be recovered from through appropriate error handling, allowing the program to continue executing or to fail gracefully. |
| Examples | - OutOfMemoryError: Indicates that the Java Virtual Machine cannot allocate memory for new objects. - StackOverflowError: Occurs when the call stack size exceeds its limit, often due to deep recursion. |
- IOException: Indicates an input/output error, such as failure to read a file. - NullPointerException: Occurs when an application attempts to use null where an object is required. |
4. Error code ( using recursive mt )
public class ErrorExample {
public static void main(String[] args) {
// This will likely cause a StackOverflowError due to recursion
recursiveMethod();
}
public static void recursiveMethod() {
// Recursive call without a base case
recursiveMethod();
}
}
5. Exception code ( filenotfound using BufferReader)
import java.io.*;
public class ExceptionExample {
public static void main(String[] args) {
try {
// Trying to read a file that doesn't exist
BufferedReader reader = new BufferedReader(new FileReader("nonexistentfile.txt"));
String line = reader.readLine();
System.out.println(line);
} catch (FileNotFoundException e) {
// Handle the exception
System.out.println("Caught an exception: " + e.getMessage());
} catch (IOException e) {
// Handle the exception
System.out.println("Caught an IOException: " + e.getMessage());
}
}
}