Kaneki
Kaneki

Reputation: 99

Moving from one catch to another try catch block

I have multiple try catch blocks. I want to make my program so that if something isn't found in the first block, the catch of that block will continue it to the other set of try catch. I did some research but wasn't able to come up with a solution.

try{
    Something
} catch (Exception e) {
    Move to next set of try catch
    }

This is what I want to achieve.

Upvotes: 3

Views: 961

Answers (5)

Jimmy
Jimmy

Reputation: 1041

@Kaneki If you want to use nested try-catch block execution in such a way that if operation is not perform inside try block then operation would be in catch block

For this you have to test for the condition whether you want to forward in catch block or not.

Note : To forward from Try block to Catch block you have to throw Exception from Try block which must be catched by Catch block.

This code may help you as per your requirement:

public void nestedTryCatch(){
        boolean wantTogoForNext = false;
        try{
            wantTogoForNext = false;
            doSomthing("Attempt-I");
            wantTogoForNext = true;
            if(wantTogoForNext)
                throw new Exception("1");
        }catch(Exception e1){
            try{
                wantTogoForNext = false;
                doSomthing("Attempt-2");
                wantTogoForNext = true;
                if(wantTogoForNext)
                    throw new Exception("2");
            }catch(Exception e2){
                try{
                    wantTogoForNext = false;
                    doSomthing("Attempt-3");
                    wantTogoForNext = true;
                    if(wantTogoForNext)
                        throw new Exception("3");
                }catch(Exception e3){
                    try{
                        wantTogoForNext = false;
                        doSomthing("Attempt-4");
                        wantTogoForNext = true;
                        if(wantTogoForNext)
                            throw new Exception("4");
                    }catch(Exception e4){
                        
                        try{
                            wantTogoForNext = false;
                            doSomthing("Attempt-5");
                            wantTogoForNext = true;
                            if(wantTogoForNext)
                                throw new Exception("5");
                        }catch(Exception e5){               
                            
                            doSomthing("Attempt-Last");
                            
                        }   
                    }
                }
            }
        }
    }



public void doSomthing(String attempt){
        System.out.println(attempt);
    }

Upvotes: 0

Subash Hamal
Subash Hamal

Reputation: 1

You can use simple try with multiple catch. Like :

try{
    something;
}catch(exception){
    Exception2;
}
catch(exception2){
    Exception2;
}


Or you can use nested try-catch like:

try{
  statement
  try{
      statement
      try{
          something
      }catch(exception1){
          Exception
      }
    }catch(exception2){
         Exception2
    }
  }catch(Exception3){
       Exception3
  }  

Upvotes: 0

Avinash tiwari
Avinash tiwari

Reputation: 269

You can add finally block below the try catch , finally will run even if some error found in try block . You can look on below code snippet and can replace your code where method1 will have all the code that you want to run in first block and so on

 try {
        method1();
    }finally {
        try {
            method2();
        }finally {
            try {
                method3();
            }finally {
                try {
                    method4();
                }finally {

                }
            }   
        }
    }

Upvotes: 1

Sidhant
Sidhant

Reputation: 16

public void tryBlock() {
    try {
        // Enter Your Code;
    } catch (Exception e) {
        System.out.println(" exception :: " + e);
        tryBlock();
    }
}

Upvotes: 0

Andreas
Andreas

Reputation: 159086

From comment:

I have 4 Elements for which I need to create try catch blocks, i.e. 4 try catch blocks

To try 4 different blocks of code, you'd write it like this:

try {
    // Attempt 1
} catch (Exception e1) {
    try {
        // Attempt 2
    } catch (Exception e2) {
        try {
            // Attempt 3
        } catch (Exception e3) {
            try {
                // Attempt 4
            } catch (Exception e4) {
                RuntimeException e = new RuntimeException("All attempts failed", e1);
                e.addSuppressed(e2);
                e.addSuppressed(e3);
                e.addSuppressed(e4);
                throw e;
            }
        }
    }
}

Upvotes: 2

Related Questions