?
Solved

Try Catch problem

Posted on 2003-03-25
6
Medium Priority
?
299 Views
Last Modified: 2010-04-15
hi all.

   i don't properly understand the use of try/catch. Also, i only have 40 points left to my name :(

i have this code, and i' can tell u what it is doing.

It fails to create an instance of the object, so it enters the FIRST catch statement, but then also enters the SECOND catch statement.
I thought if u enter a catch statement, it will exit the class operation? Yes / no?
...

public byte[] SomeOperation()
{
SomeCompany.SomeObject objSomeObject = null;

try
{
  try
  {
    objSomeObject = new SomeCompany.SomeObject();
  }
  catch( Exception e )
  {
    throw new MyOwnException( "Object failed to initilise. Error: [ " + e.Message + " ]" );
  }
}
catch ( Exception e )
{
   throw new MyOwnException( "General Exception Error occured. Error: [ " + e.Message + " ]" );
}
}
0
Comment
Question by:pure032398
[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
6 Comments
 
LVL 23

Expert Comment

by:naveenkohli
ID: 8208135
No... It will keep falling into catch blocks untill there are no more exception handlers left in the chain. In your case you have put 2 catch handlers that are catching Exception type exception.  So "throw" from first handler is going to end up in second one. You need to put handlers for different kind of handlers.

For example ....

try
{
}

catch (SqlException ex)
{
}
catch (FileException ex)
{
}
ctach (MyException ex)
{
}
catch (Exception ex)
{
}
finally
{
}
0
 
LVL 1

Author Comment

by:pure032398
ID: 8208284
so with exception handlers, once it reaches a catch, it does the code in the catch, and then leaves the try/catch block and continues on it's merry way ...

so if u have NESTED try/catch ... it will exit the nested catches and keep on working.

a catch doesn't mean it will STOP and exit the function (after the finally for that try/catch scope)?

0
 
LVL 23

Expert Comment

by:naveenkohli
ID: 8209549
If you properly handle the exception and don;t call return from there, the app will not exit from that function. but if there are no appropriate exception hanlders to take care of it, then definitely it will exit.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 8

Accepted Solution

by:
tomvergote earned 120 total points
ID: 8209761
> It fails to create an instance of the object, so it enters
> the FIRST catch statement, but then also enters the SECOND
> catch statement.
> I thought if u enter a catch statement, it will exit the
> class operation? Yes / no?

yes, but if you throw a new exception yourself in the catch-block, it will propagate to the try-catch on a higher level


try
{
 try
 {
   objSomeObject = new SomeCompany.SomeObject();
 }
 catch( Exception e )
 {
   // catch would reset the exception so the parent catch
   // doesn't get executed, but you throw a new exception
   // wich gets propagated
   throw new MyOwnException( "Object failed to initilise. Error: [ " + e.Message + " ]" );
 }
}
// this one will catch the exception you've thrown yourself
catch ( Exception e )
{
  throw new MyOwnException( "General Exception Error occured. Error: [ " + e.Message + " ]" );
}
}
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 8209807
as you wrote your original code, it is working EXACTLY as you coded it:

The inner Try block generates an execption, which is caught by the inner Catch block - and that IMMEDIATELY throws a NEW exception, which is then caught by the OUTER catch block, which THEN throws a New exception, which will be cause the code to exeit, and possibly have THAT last exception caught higher up the call tree.

In addition, if you also add a FINALLY block, then after an Exception is handled in a CATCH block, the FINALLY block code is executed (actually the FINALLY BLOCK will be Executed IN EVERY CASE, exceptions or not).

The presences of a Catch block DOES NOT always cause the code in the Procedure to terminate, it simply provides a structured mechanism to handle exceptions when and where they occur.  It is NOT necessary to have a Catch Block at the end of you code, so you could have a Procedure structured like this"
{
.
.
.
.
Try
{
  // some code that might throw an exception
}
Catch(Exception e)
{
 ///code to handle the exception - does not end the procedure, just this try block
}
finally
{
  // code that will always be executed for this block
}
.
additional code that executes after the Try block above, in all cases
.
.
}


Arthur Wood
0
 
LVL 1

Expert Comment

by:yosit
ID: 8230503
you could alter yout statment to the following:

try
{
 try
 {
   objSomeObject = new SomeCompany.SomeObject();
 }
 catch( Exception e )
 {
   throw new MyOwnException( "Object failed to initilise. Error: [ " + e.Message + " ]" );
 }
}
catch ( MyOwnException )
{
  throw;
}

catch ( Exception e )
{
  throw new MyOwnException( "General Exception Error occured. Error: [ " + e.Message + " ]" );
}
}


this will cause your second catch not to handle the inner MyOwnException , and still rethrow it our side..

but you should really plan your exception handling right, to catch only what you need , catching Exception is a bad solution.

catch only what you need ....


yosi
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

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