Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java throw e becomes null on Android

Tags:

java

android

I have the following:

     try {
        response.statusCode = urlConnection.getResponseCode();    
     } catch(IOException e) {
        throw e;
     }

I look at the debugger and e = UnknownHostException

After the throw I have:

    try {
        NetworkResponse response = NetworkHelper.getByURL(url);
     } catch(Exception e) {   <------- IT LANDS HERE, BUT e=null
        ExceptionHelper.announce(e);        
        throw e;
    }

So after the throw my catch block gets the exception but it's null. The debugger shows e=null.

I have no idea why this would happen.

like image 762
daniel Avatar asked Nov 06 '12 15:11

daniel


People also ask

Can e getMessage be null?

The getMessage() method of Throwable class is used to return a detailed message of the Throwable object which can also be null. One can use this method to get the detail message of exception as a string value.

Can throwable be null?

Returns: the cause of current Throwable. If the cause is nonexistent or unknown, it returns null.

Why is Java exception null?

What Causes NullPointerException. The NullPointerException occurs due to a situation in application code where an uninitialized object is attempted to be accessed or modified. Essentially, this means the object reference does not point anywhere and has a null value.

Should I throw exception or return null?

Throw an ExceptionThrowing is the right approach when null represents an unrecoverable failure. For example, imagine calling a helper method that gives you a database connection string. Imagine the method doesn't find the connection string, so the method gives up and returns null .


1 Answers

I don't even see the point of catching the exception if you just immediately rethrow it. Add a throws IOException to that method and let the other catch handle it.

like image 128
James McCracken Avatar answered Sep 19 '22 18:09

James McCracken