Sairam Sankaran
Sairam Sankaran

Reputation: 319

Recover from a method throwing an exception

I am fairly new to java esp. to some of the coding practices. I have a situation where a method throws a NullPointerException and the calling method catches it.

try {
    String test = Class.method(arg)
}    
catch (Exception ex) {
    ...
}

public String method(arg){
String str;
...
if(str == null) throw(exception)
return str;
}

Now some method inside Class.method(arg) throws a NullPointerException and it gets caught in the above catch (like above). Instead I want to do something like:

if (test == null) { do something else }

inside the try block.

What is the best way to handle this? Can I remove the throw inside method and make it return null?

Upvotes: 1

Views: 5420

Answers (4)

Pankaj
Pankaj

Reputation: 5250

You should catch Exception in method(arg) and return null incase of any exception.

Sample Program:

public class Test1 {

    public static void main(String[] args) {
        try{
            String test = method("1");
            if(test==null){
                //do something else
            }
        }catch(Exception e){

        }

    }

    private static String method(String str) throws Exception{
            if(str.equals("1")) return null;
            else if(str.equals("2")) throw new Exception("My Exception");
            else return str;          
    }

}

Now you can change method to something like this, it all depends on your requirement:

    private static String method(String str) throws Exception{
        try{
            if(str.equals("1")) return null;
            else if(str.equals("2")) throw new Exception("My Exception");
            else return str;
        }catch(Exception e){
            return null;
        }

    }

Upvotes: 3

Andreas Dolk
Andreas Dolk

Reputation: 114787

Expecting a null pointer exception has some bad smell. Wouldn't it be possible to add a null test in method and return the agreed result without waiting for an exception?

So instead of

 public String method(String arg) {
   try {
     return arg.toString();
   } catch (NullPointerException npe) {
     return null;
   }
 }

you really should do

 public String method(String arg) {
   if (arg == null) {
     return null;
   }

   return arg.toString();
 }

a considerable alternative would be throwing an exception other then NPE to give a hint to the problem - and because returning null is usually a bad idea:

 public String method(String arg) {
   if (arg == null) {
     throw new IllegalArgumentException("Passing null is illegal");
   }

   return arg.toString();
 }

Upvotes: 3

Richard JP Le Guen
Richard JP Le Guen

Reputation: 28753

You could specify a catch clause for the NullPointerException in particular:

try {
    String test = Class.method(arg)
}
catch (NullPointerException ex) {
    // test == null
    // do something
}
catch (Exception ex) {
    // ?
}

But you might not want to put too much logic/behavior in the catch clause, so consider just putting an if block like the one you suggested outside the try-catch block:

try {
    String test = Class.method(arg)
}
catch (NullPointerException ex) {
    // At least log the exception; never swallow exceptions. NEVER.
    System.out.println(ex);
}
catch (Exception ex) {
    // ?
}
if(test != null) {
    // do something
}
else {
    // do something else
 }

Upvotes: 0

Peter Cetinski
Peter Cetinski

Reputation: 2336

try{
    Class.method(arg);
} catch ( NullPointerException npe ) {
    //do something else
}

Upvotes: 0

Related Questions