Solved

Java Exception Handling

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 51
egit plugin on eclipse 8 82
Maven Project: Hibernate Dependencies Conflict 10 38
Java Eclipse Loop 3 20
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

808 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