---
title: "How to deal with checked exception (UnsupportedEncodingException) that can never occurs?"  
description: "How to deal with checked exception (UnsupportedEncodingException) that can never occurs?"  
author: "Anonymous User"  
published: 2014-11-16  
updated: 2014-11-17  
canonical: https://www.mindstick.com/forum/12601/how-to-deal-with-checked-exception-unsupportedencodingexception-that-can-never-occurs  
category: "android"  
tags: ["exception handling", "java", "exception"]  
reading_time: 2 minutes  

---

# How to deal with checked exception (UnsupportedEncodingException) that can never occurs?

I [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) [transform](https://www.mindstick.com/interview/511/what-are-the-steps-to-transform-xml-into-html-using-xsl) a [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) to UTF-8 encoding. But the [compilation](https://www.mindstick.com/interview/358/which-is-the-first-level-of-compilation-in-the-dot-net-languages) fails because some [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) "throws UnsupportedEncodingException".

String s = "1,2,3,4";

String smsext = new String(s.getBytes(),"UTF-8");

how to solve this ?

## Replies

### Reply by David Miller

The exception UnsupportedEncodingException is thrown by the String constructor (not by Android Studio !!!). This is a checked exception and so your code must handle it in some way.

**In this particular case** : the exception will never be thrown because "UTF-8" is hardcoded and always supported by any JVM (this is a requirement). So you can catch it silently :

```
String s = "1,2,3,4";String smsext = null;try{    smsext = new String(s.getBytes(),"UTF-8");}catch(UnsupportedEncodingException e){    //can never occurs}
```

**But I don't recommend this over simplistic approach** because silently catching an exception is almost always a very bad practice. A more appropriate solution for catched exception that *never append* is to rethrow the exception encapsulated in a non catched exception :

```
String s = "1,2,3,4";String smsext = null;try{    smsext = new String(s.getBytes(),"UTF-8");}catch(UnsupportedEncodingException e){    //can never occurs because UTF-8 is always supported    throw new RuntimeException(e);}
```

With this code, if one day you change the body of the try-catch block so that UnsupportedEncodingException can possibly occurs : the exception won't be ignored silently.


---

Original Source: https://www.mindstick.com/forum/12601/how-to-deal-with-checked-exception-unsupportedencodingexception-that-can-never-occurs

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
