Error handling

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

[C#]

I have three classes

The first class calls a method on the second class, from the second class a
method from the third class is called.

If an error is caught in the third class i simply bubble it back to the
calling function, like so: throw new Exception (ex.Message);

However from the second class i can't then do another throw new Exception...
back to the first class. It just generates a runtime error.

Any ideas why?
 
Show us the code and give us details on the error including what line it
occurred on.

Cheers
Daniel
 
FYI, if you want to bubble the same exception up, just call throw

try
{
....
}
catch(Exception)
{
throw;
}
 
Daniel said:
Show us the code and give us details on the error including what line it
occurred on.

Cheers
Daniel
Thanks for your reply Daniel

Here is the form, where I want to use my testClass
There is no actual error - Visual Studio just doesn´t show any
methods in the autocompletion dropdown.

Perhaps it´s a miscomfiguration i VS?

Public Class GpsTest
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
MyBase.Dispose(disposing)
End Sub

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.Text = "GpsTest"
End Sub

#End Region
Dim oTestClass As testClass = New testClass


End Class
 
Thanks for the responses.

Does throw; behave differently from throw new Exception(ex.Message);

??

I was under the impression they both do exactly the same thing?

FYI, if you want to bubble the same exception up, just call throw

try
{
....
}
catch(Exception)
{
throw;
}

Rob S said:
[C#]

I have three classes

The first class calls a method on the second class, from the second class
a
method from the third class is called.

If an error is caught in the third class i simply bubble it back to the
calling function, like so: throw new Exception (ex.Message);

However from the second class i can't then do another throw new
Exception...
back to the first class. It just generates a runtime error.

Any ideas why?
 
I'll have to insist. Show us the code!

To answer your new question, yes they are different.

If all you are doing is
catch{
throw; //maps to a rethrow IL statement
}
you might as well not catch the exception and let it bubble up. If you want
to do something before "throw;" then that is fine but consider whether a
finally would be a better choice.

If you want to create a new exception, then do so but make sure the
innerException points to the original so the stacktrace is not completely
lost.

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Rob S said:
Thanks for the responses.

Does throw; behave differently from throw new Exception(ex.Message);

??

I was under the impression they both do exactly the same thing?

FYI, if you want to bubble the same exception up, just call throw

try
{
....
}
catch(Exception)
{
throw;
}

Rob S said:
[C#]

I have three classes

The first class calls a method on the second class, from the second
class
a
method from the third class is called.

If an error is caught in the third class i simply bubble it back to the
calling function, like so: throw new Exception (ex.Message);

However from the second class i can't then do another throw new
Exception...
back to the first class. It just generates a runtime error.

Any ideas why?
 
Daniel, it's a bit difficult to paste my code as it's pretty long.
The principal is the same as my example.

(classes 2 and 3 are my custom classes)

My first class is my Form.
My form calls a static method in Class2 (synchronization class)
Class2 calls a static method of Class3 (sockets class)
If an error occurs in class 3 then i want a messagebox to show what the
error is, at the moment the error stops and errors on the line:
throw new Exception (ex.message); in class2, it needs to bubble up to
class1 (my form class) so i can display a message.

I think that makes sense.

Thanks for the help.

PS. Please look at my other weird problem while you're about :)


Daniel Moth said:
I'll have to insist. Show us the code!

To answer your new question, yes they are different.

If all you are doing is
catch{
throw; //maps to a rethrow IL statement
}
you might as well not catch the exception and let it bubble up. If you want
to do something before "throw;" then that is fine but consider whether a
finally would be a better choice.

If you want to create a new exception, then do so but make sure the
innerException points to the original so the stacktrace is not completely
lost.

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Rob S said:
Thanks for the responses.

Does throw; behave differently from throw new Exception(ex.Message);

??

I was under the impression they both do exactly the same thing?

FYI, if you want to bubble the same exception up, just call throw

try
{
....
}
catch(Exception)
{
throw;
}

[C#]

I have three classes

The first class calls a method on the second class, from the second
class
a
method from the third class is called.

If an error is caught in the third class i simply bubble it back to the
calling function, like so: throw new Exception (ex.Message);

However from the second class i can't then do another throw new
Exception...
back to the first class. It just generates a runtime error.

Any ideas why?
 
FYI, i'm still getting an unhandled exception error on the line:

throw; or throw new... (doesn't matter what code i use)

I just doesn't bubble back up to my main class........any ideas why?
 
Your description seems accurate, but without code to try to repro the
problem, it's hard to see what the problem could be. Could you develop a
very simple repro case and post that?

Thanks,

Ryan Chapman
..NET Compact Framework

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Does your Form class create a thread to call the method in Class2?

David Kline
Microsoft .NET Compact Framework

This posting is provided "AS IS" with no warranties, and confers no rights.


Rob S said:
Daniel, it's a bit difficult to paste my code as it's pretty long.
The principal is the same as my example.

(classes 2 and 3 are my custom classes)

My first class is my Form.
My form calls a static method in Class2 (synchronization class)
Class2 calls a static method of Class3 (sockets class)
If an error occurs in class 3 then i want a messagebox to show what the
error is, at the moment the error stops and errors on the line:
throw new Exception (ex.message); in class2, it needs to bubble up to
class1 (my form class) so i can display a message.

I think that makes sense.

Thanks for the help.

PS. Please look at my other weird problem while you're about :)


Daniel Moth said:
I'll have to insist. Show us the code!

To answer your new question, yes they are different.

If all you are doing is
catch{
throw; //maps to a rethrow IL statement
}
you might as well not catch the exception and let it bubble up. If you
want
to do something before "throw;" then that is fine but consider whether a
finally would be a better choice.

If you want to create a new exception, then do so but make sure the
innerException points to the original so the stacktrace is not completely
lost.

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Rob S said:
Thanks for the responses.

Does throw; behave differently from throw new Exception(ex.Message);

??

I was under the impression they both do exactly the same thing?

:

FYI, if you want to bubble the same exception up, just call throw

try
{
....
}
catch(Exception)
{
throw;
}

[C#]

I have three classes

The first class calls a method on the second class, from the second
class
a
method from the third class is called.

If an error is caught in the third class i simply bubble it back to
the
calling function, like so: throw new Exception (ex.Message);

However from the second class i can't then do another throw new
Exception...
back to the first class. It just generates a runtime error.

Any ideas why?
 
Back
Top