Solved

Java Exception Handling

Posted on 2006-07-17
3
223 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

760 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

21 Experts available now in Live!

Get 1:1 Help Now