Advertisement



< Prev
Next >



Throw Keyword





In Java, you may use the throw keyword when you explicitly want to throw an exception. The keyword throw is followed by an object of the exception class that you want to throw. Using throw keyword, an exception can be explicitly thrown from within the two places in a Java program -





To throw an exception out of catch block, keyword throw is followed by object reference of the exception class that was caught by catch block. For example -

Throwing an exception using the existing object reference of the exception class, IOException, from a catch block.
catch(IOException io)
{
	throw io;       // An existing exception class object referenced by "io"  of type "IOException",  is thrown.
}

In both the cases above, an object of exception class of type IOException is thrown, once out of try block and once out of catch block.

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement