Inner exception

S

stuart

Hi,

Is it possible to find out if an exception has an inner exception or not.

for example if I catch an exception of type "System.Web.HttpException"

it is likely (but not definate) that the error conatians an inner exception.

what I would like to do is determine if the exception has an inner exception
and the act on it.
I don't just want to call innerexception because I am assuming another
exception will be thrown if an inner exception does not exist.

thanks in advance for your help.

cheers

martin.
 
G

Guest

You can catch the "outer"/ main exception and then check its inner exception property like so
[C#
if(ex.InnerException != null
Response.Write(ex.InnerException.ToString())

Here are complete code samples

[C#
private void Page_Load(object sender, System.EventArgs e

tr

Method1();
}
catch(Exception ex

if(ex.InnerException != null
Response.Write(ex.InnerException.ToString())
}


void Method1(

tr

Method2();
}
catch(Exception e

throw new Exception("E2", e)
}


void Method2(

throw new Exception("E1");


[Visual Basic
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Loa
Tr
Method1(
Catch ex As Exceptio
If Not ex.InnerException Is Nothing The
Response.Write(ex.InnerException.ToString()
End If
End Try
End Su

Function Method1(
Tr
Method2(
Catch e As Exceptio
Throw New Exception("E2", e
End Try
End Function

Function Method2(
Throw New Exception("E1"
End Functio

===========
Rasika Wijayaratn
 
S

stuart

Thank you
Rasika Wijayaratne said:
You can catch the "outer"/ main exception and then check its inner exception property like so:
[C#]
if(ex.InnerException != null)
Response.Write(ex.InnerException.ToString());


Here are complete code samples:

[C#]
private void Page_Load(object sender, System.EventArgs e)
{
try
{
Method1();
}
catch(Exception ex)
{
if(ex.InnerException != null)
Response.Write(ex.InnerException.ToString());
}
}

void Method1()
{
try
{
Method2();
}
catch(Exception e)
{
throw new Exception("E2", e);
}
}

void Method2()
{
throw new Exception("E1");
}


[Visual Basic]
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top