title: How to catch a "java.net.ConnectException: Connection refused: connect" exception
body: <p>To check what happens when the server is down or the network is not working I stopped Apache and MySQL services and ran my code.<br>
  I got following error:</p>
  
  <blockquote>
    <p>java.net.ConnectException: Connection refused: connect </p>
  </blockquote>
  
  <p>how can I catch this exception in code?</p>
  
  <p>I've tried this:</p>
  
  <pre><code>public static Connection getCon(){
    Connection con=null;
  
  
    try{
      Class.forName("com.mysql.jdbc.Driver").newInstance();
      con=DriverManager.getConnection(
          "jdbc:mysql://localhost:3306/zomatocrm","root","");
    }
  
    catch(Exception e){
      System.out.println(e.getMessage());
      if(e.getCause() instanceof SQLException){
        JOptionPane.showMessageDialog(null, "Connection refused!");
      }                   
    }
    return con;
  }
  </code></pre>
  
  <p>And also this</p>
  
  <pre><code> public static Connection getCon(){
     Connection con=null;
  
     try{
       Class.forName("com.mysql.jdbc.Driver").newInstance();
       con=DriverManager.getConnection(
           "jdbc:mysql://localhost:3306/zomatocrm","root","");
     }
  
     catch(Exception e){
       System.out.println(e.getMessage());
       if(e.getCause() instanceof ConnectException){
         JOptionPane.showMessageDialog(null, "Connection refused!");
       }                   
     }
     return con;
   }
  </code></pre>
  
  <p>Also I've used the connection to do data exchange with my xampp localhost sever and then tried with stopping xamp and still getting the above exception.  </p>
  
  <p>How do I have my code catch the exception completely?</p>