?
Solved

lost exception

Posted on 2003-11-11
16
Medium Priority
?
302 Views
Last Modified: 2010-03-31
I have a simple question.
 
If an exception is generated and a new exception is generated due to re-throwing the information about the first exception is lost. What will happen to the error in the try block if the original exception is lost. I am trying to make sense of it and it is a very vague part of exceptions so if anyone can aid me in this matter I would be very happy.

Thanks,
Zac
0
Comment
Question by:sacul
[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
  • 7
  • 6
  • 3
16 Comments
 
LVL 15

Expert Comment

by:JakobA
ID: 9727881
When an exception is thrown normal processing stops, and the program exits loops, methods and whatever until arriving at a point where the thrown exception is caught. Thus it is not possible to throw another exeption until the first one has been caught.

regards JakobA
0
 
LVL 92

Expert Comment

by:objects
ID: 9727944
> and a new exception is generated due to re-throwing the
> information about the first exception is lost.

If you r simply rethrowing the exception, then no new exception is created and the first exception is not lost.
0
 
LVL 1

Author Comment

by:sacul
ID: 9727961
ok..well what if the exception is caught and re-thrown from the catch block. This result in a new exception(I think). For instance if you call the fillInStackTrace() and assign this to a variable that references an Exception. Then that Exception can be re-thrown. But if an exception is re-thrown from the catch block that is not related to the error, is this possible? and is the information encapsulated in the original object lost? will the program stop?

I was reading up on this and this is what the text said,

"Information about the exception can also be lost if while re-throwing, a new exception is created. The original exception is lost."



Thanks
Zac
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 92

Expert Comment

by:objects
ID: 9728029
Depends what you mean by re-throwing.

catch (Exception ex)
{
   throw ex;   // same exception is thrown
}


catch (Exception ex)
{
   throw new Exception("blah blah");  // new exception, original lost
}

0
 
LVL 92

Accepted Solution

by:
objects earned 80 total points
ID: 9728059
But there's nothing stopping you saving a reference to the exception or incluing it with the nex exception though:

catch (Exception ex)
{
   throw new MyException(ex);
}


catch (Exception ex)
{
   storedex = ex;
   throw new Exception("blah blah");
}

0
 
LVL 15

Expert Comment

by:JakobA
ID: 9728218
I dont get it.  The exception variable is defined as part of the catch block, and it is no different than any other block:
    {
        int i = 25;          // 'i' is local to this block just like 'ex' is local to object's catch-block abowe
        throw new Exception( "bye" );
        ...
    }
when the exception is thrown that block gets left behind and the variable i is lost.


0
 
LVL 92

Expert Comment

by:objects
ID: 9728266
not exactly, yes the variable goes out of scope. But the instance that it references is not lost.

eg.

public Object someFunction()
{
   String s = "abc";
   return s;
}
0
 
LVL 15

Expert Comment

by:JakobA
ID: 9728282
True, you can send the data out of the block before it terminate. but isnt that still bogstandard Java scope rules.
0
 
LVL 92

Expert Comment

by:objects
ID: 9728290
> but isnt that still bogstandard Java scope rules.

Yes, exceptions are simply object instances like anything else.
0
 
LVL 1

Author Comment

by:sacul
ID: 9732042
> catch (Exception ex)
  {
      throw new Exception("blah blah");  // new exception, original lost
  }

So you would never throw a new Exception in the catch block then with the risk of loosing the original one? and if you do would the execution in main stop?

thanks,

Zac
0
 
LVL 15

Assisted Solution

by:JakobA
JakobA earned 100 total points
ID: 9732275
not unless you saved the first before throwing the second (or leaving the catch block in some other way).

class TestExcep {

    static Exception[] exes = new Exception[500];
    static int EIndex = 0;

    static void someMethod( int nr ) throws Exception {
         try {
              if ( nr > 0 ) {
                   someMethod( nr-1 );
              }
              throw new Exception( "try block: exception nr " +nr );
         } catch ( Exception e ) {
              exes[ EIndex++ ] = e;
              throw new Exception( "catch block: exception nr " +nr );
        }
   }

   public static void main ( String[] args ) {
       try {
            someMethod( 5 );
       } catch ( Exception e2 ) {
            System.out.println( e2.getMessage( ) );   // the exception caught
            while ( EIndex > 0 ) {
                System.out.println( exes[--EIndex].getMessage() );   // exceptions thrown earlier
            }
       }
  }  // end of main

} //endclass TestExcep
 
regards JakobA
0
 
LVL 15

Expert Comment

by:JakobA
ID: 9732330
No, main would not stop. All that would happen when 'losing' the exception is that you would not have acces to the information it contained any more.
0
 
LVL 92

Expert Comment

by:objects
ID: 9734963
> then with the risk of loosing the original one?

There's not really any risk, you'd only do that if you did not need the original exception anymore.
0
 
LVL 1

Author Comment

by:sacul
ID: 9736252
ok I got the theoretical part of it now I just have to understand why you want to throw a new exception in a catch block, but that is for another time. I haven't been programming java for long and this is a somewhat diffuse topic. Thanks to you, experts, I know have a better understanding of it.

thank you,
Zac
0
 
LVL 92

Expert Comment

by:objects
ID: 9736327
0
 
LVL 15

Expert Comment

by:JakobA
ID: 9736436
I guess the point is that exceptions throw themselves when you try to do someting you cannot. So inside your catch-block you should be extra carefull, knowing that your program logic assumptions are already a bit dubious seeing as how the first exception got thrown.

regards JakobA
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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…
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…
Suggested Courses
Course of the Month12 days, 5 hours left to enroll

752 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