Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 302
  • Last Modified:

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 + " ]" );
}
}
0
pure032398
Asked:
pure032398
1 Solution
 
naveenkohliCommented:
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
 
pure032398Author Commented:
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
 
naveenkohliCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
tomvergoteCommented:
> 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
 
Arthur_WoodCommented:
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
 
yositCommented:
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now