Link to home
Start Free TrialLog in
Avatar of pure032398
pure032398

asked on

Try Catch problem

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 + " ]" );
}
}
Avatar of naveenkohli
naveenkohli

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
{
}
Avatar of pure032398

ASKER

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)?

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.
ASKER CERTIFIED SOLUTION
Avatar of tomvergote
tomvergote
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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