The throw statement can be used to create a custom error. It is used together with an exception type. We can throw any of the types of exceptions using throw keyword.
throw new exception;
To prevent the compile-time error in Java, we can check the exception in two ways:
The programmer uses the throws keyword to delegate the efficiency of exception handling to the caller (It might be one method or JVM). Then, the caller method is obliged to handle that exception.
The keyword throw is essentially practised to throw custom exceptions as we can throw both checked or unchecked exceptions. The keyword throw in Java is practised to explicitly throw an exception from one method or any piece of code.
The keyword throw in Java is used in the sign of the method to show that this method may throw one of the well-known type exceptions. The caller to these methods has to manage the exception by practising a try-catch block.
Knowledge2life1
website
|