?
Solved

Java Exception Handling

Posted on 2006-07-17
3
Medium Priority
?
265 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 14

Assisted Solution

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

Assisted Solution

by:objects
objects earned 400 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 1200 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month12 days, 19 hours left to enroll

777 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