In this example we will show how to use multiple catch blocks in Java.
Source Code
package com.beginner.examples;
import java.net.MalformedURLException;
import java.net.URL;
public class MultipleCatchExample {
public static void main(String[] args){
try
{
URL url = new URL("https://www.yahoo.com/");
System.out.println(url);
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception ex){
// TODO Auto-generated catch block
ex.printStackTrace();
}
}
}
Output:
https://www.yahoo.com/
References
Imported packages in Java documentation: