System.out.println() not printing

I wrote the program below but the print statement at the last is not printing anything

I have tried to check wether the value to be prited is null
this problem is from codechef here is the question, plz help me

public class Main{
    public static void main(String args[]){
        try{
            Scanner sc=new Scanner(System.in);
            int T=sc.nextInt();
            int ans[]=new int[T];
            for(int i=0;i<T;i++){
                int n=sc.nextInt();
                int a[]=new int[n];
                int d[]=new int[n];
                int gsv=0;
                for(int j=0;j<n;j++){
                    a[j]=sc.nextInt();
                }
                
                for(int j=0;j<n;j++){
                    d[j]=sc.nextInt();
                }
                
                for(int j=0;j<n;j++){
                    int ha1;
                    int ha2;
                    int hd;
                    if(j==0) {
                         ha1=a[n];
                         ha2=a[j+1];
                         hd=d[j];
                         if(hd>ha1 && hd>ha2){
                            if(hd>gsv){
                                gsv=hd;
                            } 
                         }
                    } 
                    else if(j==n-1){
                        ha1=a[j-1];
                         ha2=a[0];
                         hd=d[j];
                         if(hd>ha1 && hd>ha2){
                            if(hd>gsv){
                                gsv=hd;
                            } 
                         }    
                    }
                    else{
                        ha1=a[j-1];
                         ha2=a[j+1];
                         hd=d[j];
                         if(hd>ha1 && hd>ha2){
                            if(hd>gsv){
                                gsv=hd;
                            } 
                         }        
                    }
                    
                }
                ans[i]=gsv;
            }
            
            for(int i=0;i<T;i++){
                if(ans[i]==0){
                    System.out.println("-1");//not printing
                }else{
                    System.out.println(ans[i]);//not printing
                }
            }
        }catch(Exception e){
            
        }
    }
}

expected input:
1
4
1
1
4
1
3
4
2
1
expected output:
3

current output:
//nothings printedenter code here

Hi @ronnith ,

You are trying to assign element at the index at 4 of array ‘a’ to ha1 and it is throwing ArrayIndexOutOfBoundsException because in array ‘a’ you have elements on the index from 0 to 3.

Your below the line is the cause of the exception and your exception is getting caught in the catch block, so in the catch block, you are not doing anything that’s why it’s not printing anything.

If you want to make it run just change the assignment of ha1 = a[n] to ha1 = a[n-a]

Note: Try to call printStackTrace() method into catch block to identify your exception type.

thanks alot for your help