In this example we will show how to use throw clause in Java.
Source Code
package com.beginner.examples;
import java.net.MalformedURLException;
import java.net.URL;
public class ThrowExample {
public static void main(String[] args) throws MalformedURLException,Exception{
URL url = new URL("https://www.yahoo.com/");
if(url == null)
{
// throw exception
throw new Exception("url is null");
}
System.out.println(url);
}
}
Output:
https://www.yahoo.com/
OK
References
Imported packages in Java documentation: