Solved

Java Exception Handling

Posted on 2006-07-17
3
233 Views
Last Modified: 2010-03-31
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
 
0
Comment
Question by:winsuncareer
3 Comments
 
LVL 14

Assisted Solution

by:StillUnAware
StillUnAware earned 100 total points
ID: 17124985
The method can be found by
  ex.getStackTrace()[0].getMethodName()
0
 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
ID: 17126128
the acxtual method throwing the exception should set the message, then in your catch you can access it with getMessage()
0
 
LVL 12

Accepted Solution

by:
enachemc earned 300 total points
ID: 17128306
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groovy example issue 10 90
bitbucket vs gitbucket 3 57
advertisement module in core php 4 145
Problem to start Neon 20 87
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now