Java Nested Try Block
The try block within another try block is called nested try block.
Syntax:
try { statement ; try{ statement ; }catch (ExceptionName e){ } }catch (ExceptionName e) { }
Example
public class Nestedtryblock{ public static void main(String args[]){ try{ try{ int b =1/0; }catch(ArithmeticException e){ System.out.println(e); } try{ int num[] = {1, 2, 3, 4}; System.out.println(num[5]); }catch(ArrayIndexOutOfBoundsException e){ System.out.println(e); } System.out.println("Try block is sucessfully executed"); } catch(Exception e){ System.out.println("Exception"+e); } System.out.println("Code flow is normal"); } }
The nested try block used when part of the block may cause one error and the entire block might cause another error.
Quickly Find What You Are Looking For
Onlinetpoint is optimized for basic learning, practice and more. Examples are well checked and working examples available on this website but we can't give assurity for 100% correctness of all the content. This site under copyright content belongs to Onlinetpoint. You agree to have read and accepted our terms of use, cookie and privacy policy.