473,382 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Is a multiple try-catch-throw useful?

Hi,

I'm doing this:
try {

try {

}
catch(Exception ex){
throw;
}

}
catch(Exception ex){
throw;
}

Is a nestled try-catch-throw useful? Or is one enough?

Thanks!
Nov 17 '05 #1
7 10162
Arjen,

Why would it not be useful? Even within the "finally" clause, you can
use a try...catch statement. I do it quite often. Use try...catch
anywhere you want. The point about using it is to simply catch
exceptions, either known or unknown and exceptions can occur ANYWHERE
in your application, even in places where there is no executable code
(that is, code that you didn't necessarily write).

Best Regards
Johann Blake

Nov 17 '05 #2
The code example you have given isn't the most useful in my opinion. Haveing
nested try-catches is useful is you are actually going to do something with
the exception in the first catch (e.g. some cleanup - but this should really
be done in a finally)... in your example below your first catch statement is
doing nothing and the only result is that it is slowing your application down
in situations where an exception is thrown.

In your example you are doing nothing at all with the exceptions in either
case so I'd question why they are being caught at all... other then to make
it appear that a different method threw it.

If you have shown something like the following then it's perfectly fine and
needed in my opinion:

try {

try {

}
catch(...){
// Only catch if you're going to do something with it
}
finally{
// do some cleanup here
}

}
catch(Exception ex){

// Do something with the exception and then throw it up the stack if you
want.
throw;
}
"Arjen" wrote:
Hi,

I'm doing this:
try {

try {

}
catch(Exception ex){
throw;
}

}
catch(Exception ex){
throw;
}

Is a nestled try-catch-throw useful? Or is one enough?

Thanks!

Nov 17 '05 #3
The code example you have given isn't the most useful in my opinion. Haveing
nested try-catches is useful if you are actually going to do something with
the exception in the first catch (e.g. some cleanup - but this should really
be done in a finally)... in your example below your first catch statement is
doing nothing and the only result is that it is slowing your application down
in situations where an exception is thrown.

Actually, in your example you are doing nothing at all with the exceptions
in either catch so I'd question why they are being caught at all... other
then to make it appear that a different method threw it.

If you have shown something like the following then it's perfectly fine and
needed in my opinion:

try {

try {

}
catch(...){
// Only catch if you're going to do something with it
}
finally{
// do some cleanup here
}

}
catch(Exception ex){

// Do something with the exception and then throw it up the stack if you
want.
throw;
}
"Arjen" wrote:
Hi,

I'm doing this:
try {

try {

}
catch(Exception ex){
throw;
}

}
catch(Exception ex){
throw;
}

Is a nestled try-catch-throw useful? Or is one enough?

Thanks!

Nov 17 '05 #4
Maybe you can help me with telling me the best thing to do.

I have some code blocks (lines of code) inside a method. These blocks are
using the using-statement for data access. It will look like this:

// Start method
// Code block
// Nested codeblock
// Code block
// End method (haves return value)

This method is called from an asp.net code-behind method. If there is an
error the user must see the error.aspx file.

Where do I need to add the try-catch-statements?
For each codeblock? Or only one in the code behind-file?

Thanks!
Arjen


"Brian Delahunty" <Br************@discussions.microsoft.com> schreef in
bericht news:81**********************************@microsof t.com...
The code example you have given isn't the most useful in my opinion.
Haveing
nested try-catches is useful is you are actually going to do something
with
the exception in the first catch (e.g. some cleanup - but this should
really
be done in a finally)... in your example below your first catch statement
is
doing nothing and the only result is that it is slowing your application
down
in situations where an exception is thrown.

In your example you are doing nothing at all with the exceptions in either
case so I'd question why they are being caught at all... other then to
make
it appear that a different method threw it.

If you have shown something like the following then it's perfectly fine
and
needed in my opinion:

try {

try {

}
catch(...){
// Only catch if you're going to do something with it
}
finally{
// do some cleanup here
}

}
catch(Exception ex){

// Do something with the exception and then throw it up the stack if you
want.
throw;
}
"Arjen" wrote:
Hi,

I'm doing this:
try {

try {

}
catch(Exception ex){
throw;
}

}
catch(Exception ex){
throw;
}

Is a nestled try-catch-throw useful? Or is one enough?

Thanks!

Nov 17 '05 #5
I don't know if a try-catch is necesarry for each codeblock.

Take for example this situation.
// Layer 1
// Layer 2
// Layer 3

Layer 1 calls a method inside layer 2.
The method in layer 1 that calls the method looks like this:
...try { layer2.method() } catch {...}..

Do I need to add try-catch statement inside the other layers?

Thanks!
Arjen


"Johann Blake" <jo*********@yahoo.com> schreef in bericht
news:11**********************@g47g2000cwa.googlegr oups.com...
Arjen,

Why would it not be useful? Even within the "finally" clause, you can
use a try...catch statement. I do it quite often. Use try...catch
anywhere you want. The point about using it is to simply catch
exceptions, either known or unknown and exceptions can occur ANYWHERE
in your application, even in places where there is no executable code
(that is, code that you didn't necessarily write).

Best Regards
Johann Blake

Nov 17 '05 #6
Well if the use has to see the error.aspx page regardless of where the
exception occurred in the method and you are not doing anything else with the
exception (e.g. attempting to recover, logging it, etc) then just wrap the
"dangerous" (i.e. exception throwing part) of the method in a try-catch and
redirect to the error.aspx file in the catch.

Alternatively you could just set the ErrorPage property of the page to point
to error.aspx and not handle the exception at all. This way ASP.NET itself
will redirect to the page for you on unhandled exceptions.

hope this helps.

Brian Delahunty
Ireland

http://briandela.com/blog
"Arjen" wrote:
Maybe you can help me with telling me the best thing to do.

I have some code blocks (lines of code) inside a method. These blocks are
using the using-statement for data access. It will look like this:

// Start method
// Code block
// Nested codeblock
// Code block
// End method (haves return value)

This method is called from an asp.net code-behind method. If there is an
error the user must see the error.aspx file.

Where do I need to add the try-catch-statements?
For each codeblock? Or only one in the code behind-file?

Thanks!
Arjen


"Brian Delahunty" <Br************@discussions.microsoft.com> schreef in
bericht news:81**********************************@microsof t.com...
The code example you have given isn't the most useful in my opinion.
Haveing
nested try-catches is useful is you are actually going to do something
with
the exception in the first catch (e.g. some cleanup - but this should
really
be done in a finally)... in your example below your first catch statement
is
doing nothing and the only result is that it is slowing your application
down
in situations where an exception is thrown.

In your example you are doing nothing at all with the exceptions in either
case so I'd question why they are being caught at all... other then to
make
it appear that a different method threw it.

If you have shown something like the following then it's perfectly fine
and
needed in my opinion:

try {

try {

}
catch(...){
// Only catch if you're going to do something with it
}
finally{
// do some cleanup here
}

}
catch(Exception ex){

// Do something with the exception and then throw it up the stack if you
want.
throw;
}
"Arjen" wrote:
Hi,

I'm doing this:
try {

try {

}
catch(Exception ex){
throw;
}

}
catch(Exception ex){
throw;
}

Is a nestled try-catch-throw useful? Or is one enough?

Thanks!


Nov 17 '05 #7
Okay, thanks.

I use the enterprise library for exception handling and logging.

After the exception I want to redirect the use to the error.aspx file.

Thanks!
"Brian Delahunty" <Br************@discussions.microsoft.com> schreef in
bericht news:20**********************************@microsof t.com...
Well if the use has to see the error.aspx page regardless of where the
exception occurred in the method and you are not doing anything else with
the
exception (e.g. attempting to recover, logging it, etc) then just wrap the
"dangerous" (i.e. exception throwing part) of the method in a try-catch
and
redirect to the error.aspx file in the catch.

Alternatively you could just set the ErrorPage property of the page to
point
to error.aspx and not handle the exception at all. This way ASP.NET itself
will redirect to the page for you on unhandled exceptions.

hope this helps.

Brian Delahunty
Ireland

http://briandela.com/blog
"Arjen" wrote:
Maybe you can help me with telling me the best thing to do.

I have some code blocks (lines of code) inside a method. These blocks are
using the using-statement for data access. It will look like this:

// Start method
// Code block
// Nested codeblock
// Code block
// End method (haves return value)

This method is called from an asp.net code-behind method. If there is an
error the user must see the error.aspx file.

Where do I need to add the try-catch-statements?
For each codeblock? Or only one in the code behind-file?

Thanks!
Arjen


"Brian Delahunty" <Br************@discussions.microsoft.com> schreef in
bericht news:81**********************************@microsof t.com...
> The code example you have given isn't the most useful in my opinion.
> Haveing
> nested try-catches is useful is you are actually going to do something
> with
> the exception in the first catch (e.g. some cleanup - but this should
> really
> be done in a finally)... in your example below your first catch
> statement
> is
> doing nothing and the only result is that it is slowing your
> application
> down
> in situations where an exception is thrown.
>
> In your example you are doing nothing at all with the exceptions in
> either
> case so I'd question why they are being caught at all... other then to
> make
> it appear that a different method threw it.
>
> If you have shown something like the following then it's perfectly fine
> and
> needed in my opinion:
>
> try {
>
> try {
>
> }
> catch(...){
> // Only catch if you're going to do something with it
> }
> finally{
> // do some cleanup here
> }
>
> }
> catch(Exception ex){
>
> // Do something with the exception and then throw it up the stack if
> you
> want.
> throw;
> }
>
>
> "Arjen" wrote:
>
>> Hi,
>>
>> I'm doing this:
>> try {
>>
>> try {
>>
>> }
>> catch(Exception ex){
>> throw;
>> }
>>
>> }
>> catch(Exception ex){
>> throw;
>> }
>>
>> Is a nestled try-catch-throw useful? Or is one enough?
>>
>> Thanks!
>>
>>
>>


Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Ohaya | last post by:
Hi, I'm trying to understand a situation where ASP seems to be "blocking" of "queuing" requests. This is on a Win2K Advanced Server, with IIS5. I've seen some posts (e.g.,...
4
by: Jeff | last post by:
We have multiple ASP.Net web apps in development. As a standard we are looking to go with SQL Server to hold state information. Can we have the multiple apps all point to a single State DB? Or...
9
by: Graham | last post by:
I have been having some fun learning and using the new Controls and methods in .Net 2.0 which will make my life in the future easier and faster. Specifically the new databinding practises and...
2
by: jasonsgeiger | last post by:
From: "Factor" <jasonsgeiger@gmail.com> Newsgroups: microsoft.public.in.csharp Subject: Multiple Clients, One port Date: Wed, 19 Apr 2006 09:36:02 -0700 I'm been working with sockets for a...
4
by: Matt Ratliff | last post by:
Hello, I would appreciate any assistance you have with the following problem: I have (as an example) an array of values as follows: arrayvalues=new Array("0001","0003","0005") where each is the...
35
by: keerthyragavendran | last post by:
hi i'm downloading a single file using multiple threads... how can i specify a particular range of bytes alone from a single large file... for example say if i need only bytes ranging from...
3
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is...
2
by: Paul McGuire | last post by:
On May 25, 8:37 am, Michael Hines <michael.hi...@yale.eduwrote: Here's a more general version of your testing code, to detect *any* diamond multiple inheritance (using your sample classes). --...
4
by: Enrico | last post by:
Hi there, I have the following situation (I tryed to minimize the code to concentrate on the issue): def __getattr__(self, name): print 'A.__getattr__' if name == 'a': return 1 raise...
43
by: bonneylake | last post by:
Hey Everyone, Well this is my first time asking a question on here so please forgive me if i post my question in the wrong section. What i am trying to do is upload multiple files like gmail...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.