-4

public class ApplicationException extends Exception 
{
    private static final long serialVersionUID = 1L;

    public ApplicationException()
    {
        super();
    }

    public ApplicationException(String message)
    {
        super(message);
    }
}

public class Utilities 
{
    public static byte[] ParseHehadecimalString(String s)    // error 1
    {
        throw new ApplicationException("ParseHehadecimalString not implemented");
    }
}

public class Client extends Activity {
{
    public void OnBtnSendClick(View v)
    {
        String s = et_client_out.getText().toString();
        byte[] bytes;

        try
        {
            bytes = Utilities.ParseHehadecimalString(s);
        }
        catch(ApplicationException ex)    // error 2
        {
            Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_SHORT).show();
            return;
       }
}

Error 1: Unhandled exception type ApplicationException

Error 2: Unreachable catch block for ApplicationException. This exception is never thrown from the try statement body

How this can be fixed?

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Alex F
  • 42,307
  • 41
  • 144
  • 212
  • 1
    4 downvotes without any comments? I am trying to guess what is so stupid in my question... – Alex F Oct 01 '12 at 08:24

5 Answers5

2

error 1 needs to look like

public static byte[] ParseHehadecimalString(String s) throws ApplicationException {

error 2 does not through this exception because of error 1. Once error 1 is fixed errror 2 will disappear

This link describes how to add exceptions to method signatures.

RNJ
  • 15,272
  • 18
  • 86
  • 131
2

Declare method throws your Exception. Here is the nice link

public static byte[] ParseHehadecimalString(String s)  throws ApplicationException
Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72
2

Try

public static byte[] ParseHehadecimalString(String s) throws ApplicationException

This way you will declare your method could throw that exception.

Hans Hohenfeld
  • 1,729
  • 11
  • 14
2

You forgot to declare that your Methods throws ApplicationException:

public static byte[] ParseHehadecimalString(String s) throws ApplicationException

You may want to consider using an IDE like Eclipse, that will point out such Errors straight away.

dngfng
  • 1,923
  • 17
  • 34
2

In Java there are checked and unchecked exceptions. Your exception extends from Exception, thus it is a checked exception. Unchecked exceptions extend from RuntimeException.

Checked exceptions need to be declared in the method header, so that the compiler knows that the method may throw this exception. You did not do this, so the compiler thinks that the method can not throw this exception. Unchecked exceptions may be thrown without declaring them in the method header.

Community
  • 1
  • 1
Sjoerd
  • 74,049
  • 16
  • 131
  • 175