.NETGURU
try catch
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngarchitecture' list.


Yannick Smits
I was wondering why the catch part is compulsory when doing a try like in:
try {
do something;
}
catch
{}

I usually end up leaving the catch part blank (like above) witch doesn't
look right.

Yannick Smits

Reply to this message...
 
    
Minh Truong
[Original message clipped]

as I understand try/catch, this structure is the default behavior, if you
didn't specify any try/catch

try
{
// Do something
}
catch (Exception exc)
{
throw e;
}

So that if any exception occurs in your function, it is propagaded up the
call stack.
In your case, where the catch block is empty, this means that DON'T
propagade the exception up the chain, but instead ignore any exceptions
raised.
There is sometimes a need to ignore exceptions, it's just not a good idea to
do it on a wide scale.

Reply to this message...
 
    
Russ McClelland
Because if you are going to "try" something and not "catch" the
potential errors, why wrap it in a try block to begin with. These two
are (almost)functionally the same thing:

1.
try
{
anObject.DoSomethingFun();
anObject.DomMoreFunStuff();
}
catch( Exception e )
{
//Do nothing with the error so we proceed like nothing happened.
}
Return;

2.

anObject.DoSomethingFun();
anObject.DomMoreFunStuff();
return;

There is a difference between them though. If line 1 throws an error in
the first example, line 2 is never executed. If line 1 throws an error
in the second example, line 2 is executed before the method returns. =20

-----Original Message-----
From: Yannick Smits [mailto:Click here to reveal e-mail address]=20
Sent: Thursday, July 11, 2002 5:22 PM
To: aspngarchitecture
Subject: [aspngarchitecture] try catch

I was wondering why the catch part is compulsory when doing a try like
in: try {
do something;
}
catch
{}

I usually end up leaving the catch part blank (like above) witch doesn't
look right.

Yannick Smits

| [aspngarchitecture] member Click here to reveal e-mail address =3D YOUR ID=20
| http://www.asplists.com/asplists/aspngarchitecture.asp =3D JOIN/QUIT=20
| http://www.asplists.com/search =3D SEARCH Archives

Reply to this message...
 
    
Kirk Jackson

One situation that you use a 'try' without a 'catch' is using a
'finally' if you want to guarantee that some code is run, even if there
is an exception thrown:

try
{
// do stuff
}
finally
{
// tidy up afterwards
}

I agree with Minh's comments about ignoring exceptions - it can be
really frustrating trying to track down an obscure error, only to find
that an exception was thrown and immediately ignored.

Kirk

-----Original Message-----
From: Russ McClelland [mailto:Click here to reveal e-mail address]=20
Sent: Friday, 12 July 2002 1:25 p.m.
To: aspngarchitecture
Subject: [aspngarchitecture] RE: try catch

Because if you are going to "try" something and not "catch" the
potential errors, why wrap it in a try block to begin with. These two
are (almost)functionally the same thing:

1.
try
{
anObject.DoSomethingFun();
anObject.DomMoreFunStuff();
}
catch( Exception e )
{
//Do nothing with the error so we proceed like nothing happened. }
Return;

2.

anObject.DoSomethingFun();
anObject.DomMoreFunStuff();
return;

There is a difference between them though. If line 1 throws an error in
the first example, line 2 is never executed. If line 1 throws an error
in the second example, line 2 is executed before the method returns. =20

-----Original Message-----
From: Yannick Smits [mailto:Click here to reveal e-mail address]=20
Sent: Thursday, July 11, 2002 5:22 PM
To: aspngarchitecture
Subject: [aspngarchitecture] try catch

I was wondering why the catch part is compulsory when doing a try like
in: try {
do something;
}
catch
{}

I usually end up leaving the catch part blank (like above) witch doesn't
look right.

Yannick Smits

| [aspngarchitecture] member Click here to reveal e-mail address =3D YOUR ID
| http://www.asplists.com/asplists/aspngarchitecture.asp =3D JOIN/QUIT=20
| http://www.asplists.com/search =3D SEARCH Archives

| [aspngarchitecture] member Click here to reveal e-mail address =3D YOUR ID
| http://www.asplists.com/asplists/aspngarchitecture.asp =3D JOIN/QUIT
| http://www.asplists.com/search =3D SEARCH Archives

Reply to this message...
 
    
Minh Truong
[Original message clipped]

I found this to be not the case. If line 1 raises an exception, the method
is exited immediately.
Can anyone coraborate?

Reply to this message...
 
    
Damon Allison
Be careful, there is a little difference between
try
{}
catch (Exception exc)
{ throw e; }

and
try
{}
catch (Exception exc)
{ throw; }

The throw e; I believe throws a new exception. If you say throw; you keep
the original call stack where the error executed. If you say throw e; the
call stack starts over from the current stack frame. It seems if you are
going to raise and throw the same exception, you probably want to use throw;
not throw e; Hopefully someone with a better understanding can elaborate.

Damon

----- Original Message -----
From: "Minh Truong" <Click here to reveal e-mail address>
To: "aspngarchitecture" <Click here to reveal e-mail address>
Sent: Thursday, July 11, 2002 5:40 PM
Subject: [aspngarchitecture] Re: try catch

> > I was wondering why the catch part is compulsory when doing a try like
in:
[Original message clipped]

Reply to this message...
 
    
Yannick Smits
>why wrap it in a try block to begin with.
I use it for IO access. For instance in a photogallery witch generates
thumbnails you don't want your users to see an error message when an admin
is uploading a new file for the gallery ("File is being used by an other
process").
In fact I adore the "on error resume next" VBscript function and this is my
.NET way to achieve it.
Is this wrong design?

Thanks,
Yannick Smits

"Russ McClelland" <Click here to reveal e-mail address> wrote in message
news:682523@aspngarchitecture...

Because if you are going to "try" something and not "catch" the
potential errors, why wrap it in a try block to begin with. These two
are (almost)functionally the same thing:

1.
try
{
anObject.DoSomethingFun();
anObject.DomMoreFunStuff();
}
catch( Exception e )
{
//Do nothing with the error so we proceed like nothing happened.
}
Return;

2.

anObject.DoSomethingFun();
anObject.DomMoreFunStuff();
return;

There is a difference between them though. If line 1 throws an error in
the first example, line 2 is never executed. If line 1 throws an error
in the second example, line 2 is executed before the method returns.

-----Original Message-----
From: Yannick Smits [mailto:Click here to reveal e-mail address]
Sent: Thursday, July 11, 2002 5:22 PM
To: aspngarchitecture
Subject: [aspngarchitecture] try catch

I was wondering why the catch part is compulsory when doing a try like
in: try {
do something;
}
catch
{}

I usually end up leaving the catch part blank (like above) witch doesn't
look right.

Yannick Smits

| [aspngarchitecture] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngarchitecture.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

Reply to this message...
 
    
SHEATHER,Kristoffer (DITR)
Yes this is correct.

-----Original Message-----
From: Damon Allison [mailto:Click here to reveal e-mail address]=20
Sent: Friday, July 12, 2002 1:21 PM
To: aspngarchitecture
Subject: [aspngarchitecture] Re: try catch

Be careful, there is a little difference between
try
{}
catch (Exception exc)
{ throw e; }

and
try
{}
catch (Exception exc)
{ throw; }

The throw e; I believe throws a new exception. If you say throw; you
keep the original call stack where the error executed. If you say throw
e; the call stack starts over from the current stack frame. It seems if
you are going to raise and throw the same exception, you probably want
to use throw; not throw e; Hopefully someone with a better understanding
can elaborate.

Damon

----- Original Message -----
From: "Minh Truong" <Click here to reveal e-mail address>
To: "aspngarchitecture" <Click here to reveal e-mail address>
Sent: Thursday, July 11, 2002 5:40 PM
Subject: [aspngarchitecture] Re: try catch

[Original message clipped]

to
[Original message clipped]

| [aspngarchitecture] member Click here to reveal e-mail address =3D YOUR ID

| http://www.asplists.com/asplists/aspngarchitecture.asp =3D JOIN/QUIT=20
| http://www.asplists.com/search =3D SEARCH Archives

Notice:
The information contained in this e-mail message and any attached files may
be confidential information, and may also be the subject of legal
professional privilege. If you are not the intended recipient any use,
disclosure or copying of this e-mail is unauthorised. If you have received
this e-mail in error, please notify the sender immediately by reply e-mail
and delete all copies of this transmission together with any attachments.

Reply to this message...
 
    
Russ McClelland
Doh, right, I don't know what I was thinking. It throws the exception
up to the next method in the call stack progressively looking for an
exception handler. If it doesn't find one, it terminates the program...

-----Original Message-----
From: Minh Truong [mailto:Click here to reveal e-mail address]=20
Sent: Thursday, July 11, 2002 9:36 PM
To: aspngarchitecture
Subject: [aspngarchitecture] RE: try catch

[Original message clipped]

I found this to be not the case. If line 1 raises an exception, the
method is exited immediately. Can anyone coraborate?

| [aspngarchitecture] member Click here to reveal e-mail address =3D YOUR ID=20
| http://www.asplists.com/asplists/aspngarchitecture.asp =3D JOIN/QUIT=20
| http://www.asplists.com/search =3D SEARCH Archives

Reply to this message...
 
    
Minh Truong
[Original message clipped]

I mis-typed. That should've been "throw exc;" instead of "throw e;"
But I didn't know the functionality of "throw;"

Tested it out and it looks like it's the same Exception raised from below
that's being thrown regardless of whether it's "throw exc;" or "throw;".
But like you say, use "throw;" to preserve the stack trace. That's a good
one to know.

Reply to this message...
 
    
Minh Truong
[Original message clipped]

You can elect to ignore a specific exception & pass up everything else like:

try
{
// Do stuff
}
catch (IOException exc)
{
// Ignore with empty block
}
catch (Exception exc)
{
throw; // Pass it up
}

You may even get more specific by checking for the "File is being used..."
exception.
Note, the order of the catch blocks is important. You want to order more
specific exception first, because the compiler will enter the first catch
block that matches the exception.

Reply to this message...
 
 
System.IO.IOException




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification