throw keyword in java

In Java, the throw keyword is used to explicitly throw an exception. Here’s a brief explanation:

  1. Throwing an Exception:
  • The throw keyword is followed by an instance of an exception.
  • This instance can be a built-in exception or a custom exception that extends the Exception class or one of its subclasses.
   throw new SomeException("This is an example exception");
  1. Catching the Exception:
  • When an exception is thrown, it can be caught and handled using a try-catch block.
  • The catch block specifies the type of exception it can handle.
   try {
       // code that may throw an exception
   } catch (SomeException e) {
       // code to handle the exception
   }

Here’s an example that demonstrates the use of throw and try-catch:

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

   public class Example {
       public static void main(String[] args) {
           try {
               // Some condition that triggers an exception
               if (someCondition) {
                   throw new CustomException("Custom exception occurred");
               }

               // Rest of the code
           } catch (CustomException e) {
               System.err.println("Caught an exception: " + e.getMessage());
               // Handle the exception or take appropriate action
           }
       }

In this example, if the someCondition is true, a CustomException is thrown. The catch block then handles this exception and executes the code within the catch block.

Remember that throwing exceptions should be done for exceptional conditions or errors, and it’s important to handle exceptions appropriately to ensure the robustness of your code.

Throw in java is used to handle exception explicitly

Generall form of throw

throw ThrowableInstance;

ThrowableInstance must be an object of type Throwable or a subclass of Throwable. Primitive types, such as int or char, as well as non-Throwable classes, such as String and Object, cannot be used as exceptions. There are two ways you can obtain a Throwable object: using a parameter in a catch clause, or creating one with the new operator

The flow of execution stops immediately after the throw statement; any subsequent statements are not executed. The nearest enclosing try block is inspected to see if it has a catch statement that matches the type of exception. If it does find a match, control is transferred to that statement. If not, then the next enclosing try statement is inspected, and so on. If no matching catch is found, then the default exception handler halts the program and prints the stack trace

Demonstrate throw

class ThrowDemo {
static void demoproc() {
try {
throw new NullPointerException("demo");
} catch(NullPointerException e) {
System.out.println("Caught inside demoproc.");
throw e; // rethrow the exception
}
}
public static void main(String args[]) {
try {
demoproc();
} catch(NullPointerException e) {
System.out.println("Recaught: " + e);
}
}
}

Caught inside demoproc.
Recaught: java.lang.NullPointerException: demo

throw new NullPointerException(“demo”);

Leave a Comment