0

I ran into a strange problem with try catch which got me doubting about my own realization of exception handling fundamentals. As per the basic syntax

try{
      code to  be checked     
   }
catch(Exception e){}
finally{}

However the code below gives me a null pointer exception which I believe should have been caught.

class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{



    try{

            for(Model m: Null Collection coming from DB)
            System.out.println("Inner Block");

      System.out.println("Outer Block");        

    }catch(Exception e){}

}
}
aman shivhare
  • 334
  • 2
  • 13

5 Answers5

1

The following snippet prints "An exception!"

List<String> strings = null;
try {
    for (String s : strings) {
        System.out.println(s);
    }
} catch (Exception e) {
    System.out.println("An exception!");
}

And as others have pointed out, and you stated yourself, Runtime exceptions are caugth by Exception catches

Have you tried recompiling all your code from scratch? In my team (250.000 lines codebase) using eclipse we sometimes have trouble with bad compiles that can give unexplainable problemes like this. We usually solve them by a complete recompile.

Community
  • 1
  • 1
0

Are you sure about your issue? I believe there's some clean-up code in your catch which hasn't been initialized yet - hence the exception. Would help if you post your actual code. The following works as expected:

import java.util.List;

public class Main {

public static void main(String[] args) {
    List i=null;
    try{
        for(Object o: i) {
            System.out.println("Inner Block");
        }
        System.out.println("Outer Block");
    }catch(Exception e){}
}

}

Vinay Rao
  • 1,284
  • 9
  • 13
0

Understanding the Try Catch block behavior?

The main reason to use try-catch block is to handle something will goes wrong with your code or something you are unexpected for the normal case and something will throw an exception somehow and to handle it in catch block, and the finally block is used almost to close any opened stream and it will run every time in the code even if the try or catch returned any value (except for system termination).

In your code it seems that there is something you get it from database which is null or not initialized yet or it is already returned from database of null value, Now you cannot use null object if it's not initialized already! you have to insure if it's null or not then use it like this:

class Ideone {
    public static void main(String[] args) throws java.lang.Exception {
        try {
            if(Null Collection coming from DB != null){
                for(Model m: Null Collection coming from DB)
                    System.out.println("Inner Block");
            }
            System.out.println("Outer Block");

        } catch (Exception e) {}
    }
}

The NullPointerException is extends RuntimeException and it's thrown when you try to use a null object.

Husam
  • 2,069
  • 3
  • 19
  • 29
-1

i has not been initialized,

public class Ideone{
public static void main (String[] args) throws java.lang.Exception
{

    int i = 0;

    try{ 


            if(i<2)
            System.out.println("Inner Block");

      System.out.println("Outer Block");        

    }catch(Exception e){}

}
}
codedude
  • 1
  • 3
-2

Cannot reproduce. I tried this

public static void main (String[] args) throws java.lang.Exception
    {
        Collection c = null ;
        try{ 
            for(Object i:c)
                System.out.println("Inner Block");
            System.out.println("Outer Block");        
        }catch(Exception e){}
    }

It works well.

Harper Koo
  • 597
  • 5
  • 14
  • 2
    It would print "error" in the console whether or not an exception is thrown. – Andy Turner Nov 09 '15 at 07:01
  • It not gives me a null pointer exception, I delete the last line – Harper Koo Nov 09 '15 at 07:05
  • 1
    Maybe try putting the `System.out.println("error");` in the `catch` block, then you'd see the exception. http://ideone.com/pagkGm – Andy Turner Nov 09 '15 at 07:06
  • I know what you mean. But it really can catch the exception instead of throw it out. – Harper Koo Nov 09 '15 at 07:11
  • Yes, it catches an exception if one occurs. Your statement that it doesn't give a null pointer exception is simply wrong: a NPE is thrown, however you swallow it without giving any indication that it occurred. – Andy Turner Nov 09 '15 at 07:31
  • I'm confused now. You mean this func throws a NPE? – Harper Koo Nov 09 '15 at 07:41
  • An NPE is thrown in this code; however, you can't see that because you swallow the exception. There is no way to discern whether it is thrown or not at runtime. – Andy Turner Nov 09 '15 at 07:44