Solved

Java Exception Handling

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java DateChooser? 3 36
more than one jdk and one jre 1 41
couple of eclipse 5 36
login form jsp example 2 24
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)
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

786 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