Error vs Exception - Yash-777/MyWorld GitHub Wiki

public class Throwable implements Serializable gitlink

image https://seagence.com/blog/java-exceptions-handling/

Error Class (Cannot handled by the program): When a dynamic linking failure or other hard failure in the Java virtual machine occurs, the virtual machine throws an Error. Simple programs typically do not catch or throw Errors.

Exception Class (can be handled by the program): Most programs throw and catch objects that derive from the Exception class. An Exception indicates that a problem occurred, but it is not a serious system problem. Most programs you write will throw and catch Exceptions as opposed to Errors.

What is the difference between exception and error in Java?

Errors typically happen while an application is running. For instance, Out of Memory Error occurs in case the JVM runs out of memory. On the other hand, exceptions are mainly caused by the application. For instance, Null Pointer Exception happens when an app tries to get through a null object.



Exceptions docs.oracle.com

What Is an Exception?

An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.

Java exception is an unusual condition that generally occurs in a code sequence while the program is being executed. And, when an exception occurs in your code, it completely disrupts the normal flow of the program and abnormally terminates the process, so it must be handled correctly.

The term exception is shorthand for the phrase "exceptional event."

When an error occurs within a method, the method creates an object and hands it off to the runtime system. The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred. Creating an exception object and handing it to the runtime system is called throwing an exception.

After a method throws an exception, the runtime system attempts to find something to handle it. The set of possible "somethings" to handle the exception is the ordered list of methods that had been called to get to the method where the error occurred. The list of methods is known as the call stack

image

The runtime system searches the call stack for a method that contains a block of code that can handle the exception. This block of code is called an exception handler. The search begins with the method in which the error occurred and proceeds through the call stack in the reverse order in which the methods were called. When an appropriate handler is found, the runtime system passes the exception to the handler. An exception handler is considered appropriate if the type of the exception object thrown matches the type that can be handled by the handler.

The exception handler chosen is said to catch the exception. If the runtime system exhaustively searches all the methods on the call stack without finding an appropriate exception handler, as shown in the next figure, the runtime system (and, consequently, the program) terminates.

image

Using exceptions to manage errors has some advantages over traditional error-management techniques. You can learn more in the Advantages of Exceptions section.


Exception Handling

Exception handling is the process of responding to unwanted or unexpected events when a computer program runs. Exception handling deals with these events to avoid the program or system crashing, and without this process, exceptions would disrupt the normal operation of a program.

How do you handle exceptions in Java?

Exception handling can be performed using try, catch, and finally blocks

Try blocks should include code that might throw an exception. If your code throws more than one exception, you can choose whether to:

  • Use a separate try block for every statement that may throw an exception or
  • Use one try block for several statements that could throw multiple exceptions.

Each Catch block should handle exceptions thrown by the try blocks. The finally block gets executed whether or not an exception is thrown after the successful execution of the try block or after one of the catch blocks.

There is no limit to add catch blocks, as you can include as many Catch blocks as you want. However, you can add only one finally block to each try block.

The runtime system always executes the statements within the finally block regardless of what happens within the try block. So it's the perfect place to perform cleanup.

try {

} catch (IndexOutOfBoundsException e) { // catch (ExceptionType name)
    System.err.println("IndexOutOfBoundsException: " + e.getMessage());
} catch (IOException e) {
    System.err.println("Caught IOException: " + e.getMessage());
}

Note: The finally block may not execute if the JVM exits while the try or catch code is being executed.


Types of Exceptions in Java

Generally, there are two kinds of Java exceptions, namely:

Checked exceptions (compile-time exceptions): compiler checks whether the exception is handled and throws an error accordingly. Unchecked exceptions (runtime exceptions): Occur during program execution. These are not detectable during the compilation process.

Checked exceptions. Also called compile-time exceptions, the compiler checks these exceptions during the compilation process to confirm if the exception is being handled by the programmer. If not, then a compilation error displays on the system.

The class Exception and any subclasses that are not also subclasses of RuntimeException are checked exceptions

Some of the most common Checked exceptions in Java include:

class IOException extends Exception   [CharConversionException, EOFException, FileNotFoundException, SSLException, 
ObjectStreamException, ObjectStreamException]
ParseException, InterruptedException, InvocationTargetException, NoSuchMethodException, SQLException, ClassNotFoundException.
  • IOException: Signals that an I/O exception of some sort has occurred. This class is the general class of exceptions produced by failed or interrupted I/O operations.
  • SOAPException

Unchecked exceptions. Also called runtime exceptions, these exceptions occur during program execution. These exceptions are not checked at compile time, so the programmer is responsible for handling these exceptions. Unchecked exceptions do not give compilation errors.

RuntimeException and its subclasses are unchecked exceptions. class RuntimeException extends Exception

Some of the most common Unchecked exceptions in Java include:

class ArithmeticException extends RuntimeException
class IndexOutOfBoundsException extends RuntimeException  [ArrayIndexOutOfBoundsException, StringIndexOutOfBoundsException]
NumberFormatException, ArithmeticException, ClassCastException, IllegalArgumentException, IllegalStateException
  • NullPointerException: Thrown when an application attempts to use null in a case where an object is required.
  • ArithmeticException: Thrown when an exceptional arithmetic condition has occurred. When any integet/number is "divide by zero" throws an instance of this class.
  • IndexOutOfBoundsException: Thrown to indicate that an index of some sort (such as to an array, to a string, or to a vector) is out of range.
  • ClassCastException, ConcurrentModificationException, FileSystemNotFoundException, NoSuchElementException, UnsupportedOperationException, UnmodifiableSetException, DOMException

Why do we need exception handling in Java?

If there is no try and catch block while an exception occurs, the program will terminate. Exception handling ensures the smooth running of a program without program termination.


Throwing exceptions

In Java, throwing exceptions is a specific programming technique. An exception can only be caught if it was thrown before, either by the Java platform or by custom code. In Java, exceptions are thrown with the throw statement. For example, ArrayIndexOutOfBoundsException, NullPointerException, and IOException were all thrown automatically by the Java platform.

  1. The Throw Statement

Throwable objects that inherit from the Throwable class can be used with the throw statement. Any block of code explicitly throws an exception. A catch block must follow each throw statement inside a try block.

  1. The Throws Statement

Java has a throws statement in addition to a throw. Any method that might throw a checked exception can use it in its signature. As an example, let’s say that the method doesn’t want to and cannot handle a checked exception that will prevent the code from compiling. With the throws keyword, you can indicate that the caller needs to handle this exception in its own try-catch block.


Handling NullPointerException?

NullPointerExceptionseagence.com blog is a kind of Runtime exception that occurs when you try to access a variable that doesn’t point to any object and refers to nothing or null.

Following are the most common situations for the java null pointer exception occurrence.

  • When null parameters are passed on to a method.
  • Trying to access the properties or an index element of a null object.
  • Calling methods on a null object. String str = null; str.length(); if ("Yash".equals(str))
  • Following incorrect configuration for dependency injection frameworks such as Spring.
public static int getLength(String s) {
  if (s == null)
    throw new IllegalArgumentException("The argument cannot be null");
  return s.length();
}

⚠️ **GitHub.com Fallback** ⚠️