I have below code use to `POST` JSON object to the following URL

```
        HttpEntity messageEntity = new HttpEntity(message, buildHttpHeaders(getTerminalId()));
        String theUrl = "http://123.433.234.12/receive";
        try {
               System.out.println("In try block");
               ResponseEntity<Dto> responseEntity= restTemplate.exchange(theUrl, HttpMethod.POST, messageEntity, Dto.class);
          } catch (HttpStatusCodeException ex) {
              // get http status code
          }

```

If the URL is invalid or service unavailable, I want it throw error status code like 404 or 503. Unfortunatelly it will always stop at the `try` block..Is there a way to solve that ? 

**Output**

```
In try block

```

**Edit**

```
 String theUrl = "http://123.433.234.12/receive" + transactionId; //invalid Id
   try {
          System.out.println("=========start=========");
          ResponseEntity<Dto> responseEntity= restTemplate.exchange(theUrl, HttpMethod.POST, messageEntity, Dto.class);
          System.out.println("=========end=========");
       } catch (HttpStatusCodeException ex) {
          String a = ex.getStatusCode().toString();
          System.out.println(a);
    }

```

Output

```
=========start=========
2017-09-22 14:54:54 [xles-server-ThreadPool.PooledThread-0-running] ERROR c.r.abc.jpos.JposRequestListener - Error HttpStatusCode 500org.springframework.web.client.HttpServerErrorException: 500 null

```

It stop and not display `========end ========` or any status code in `catch` block

Valid url

```
   http://abc0/receive/hello

```

If I change to 

```
 http://abc0/recei/hello

```

I will get `404` in catch block, it look fine. But when I change to another url that not exits,example

```
 http://123.433.234.12/receive

```

it is in try block .Why ????