java - How to use mult-catch clause? -


this question has answer here:

class demo{ public static void main(string args[]){     int x=3,n=5,d=0;     int ar[]=new int[3];     string name="neno";      system.out.println("start main");     try{         ar[x]=name.charat(n)/d; //n=5     }catch(stringindexoutofboundsexception e){         system.out.println("string index error");     }catch(runtimeexception e){         system.out.println("any runtime error");     }catch(arrayindexoutofboundsexception e){         system.out.println("array index error");     }catch(arithmeticexception e){         system.out.println("arithmetic error");     }      system.out.println("end   main"); } } 

i used code filter exceptions,but there error in code. says remove catch-clauses of arrayindexoutofbounds , arithmeticexception. because order of catch-clauses error springs up? when change order this...

class demo{ public static void main(string args[]){     int x=3,n=5,d=0;     int ar[]=new int[3];     string name="niroth";      system.out.println("start main");     try{         ar[x]=name.charat(n)/d; //n=5     }catch(stringindexoutofboundsexception e){         system.out.println("string index error");     }catch(arrayindexoutofboundsexception e){         system.out.println("array index error");     }catch(arithmeticexception e){         system.out.println("arithmetic error");     }catch(runtimeexception e){         system.out.println("any runtime error");     }catch(exception e){         system.out.println("any error");     }      system.out.println("end   main"); } } 

there no error in order. can explain me reason issue?

catching exceptions buckets placed 1 after another.you must cacth broader ones in last. hope following diagram helps:

wrong:

 \                           /   \_____runtimeexception____/        \                  /        \__aiobexception_/        //arrayindexoutofbounds        \                 /        \__ariexception_/        //arithmeticexception 

correct:

      \                  /        \__aiobexception_/        //arrayindexoutofbounds        \                 /        \__ariexception_/        //arithmeticexception    \                           /   \_____runtimeexception____/ 

Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -