In main method, I have
String errorMsg = null;
MainXXX{
try{
this.m1();
this.m2();
this.m3();
this.m4();
this.m5();
}catch(Exception ex)
{
System.out.println("Exception: "+ ex);
//this is what I want to do.
// if exception occurs in m1 --->set errorMsg "Exception in method m1";
//if exception occurs in m2 --> set errorMsg "Exception in method m2";
........
System.out.println("Error Message: "+errorMsg);
}
All the submethods, m1, m2... have return type as void.
Now, according to this try-catch block, here if any exception occurs in any of the methods, the rest of the methods should not execute.
How do I set the error Message and print exactly
You could increase some counter after each method execution, and determine by it's value which method failed.
You could use an error flag (set initialy to false), and each method will be executed in it's own try ... catch and depend of the error flag beeing false. When an error ocurs, set the error flag to true -> will cause all subsequent functions to not beeing executed.
0
Question has a verified solution.
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
ex.getStackTrace()[0].getM