# to throw an exception:
throw SomeException(arg1, arg2)
throw Exception('my message')
# to declare your own exception class:
class FooException
inherits Exception
def init(message as String)
.init(message, nil)
def init(message as String, innerExc as Exception)
base.init(message, innerExc)
# to catch exceptions:
try
# do some stuff
catch ioe as IOException
print 'I/O error:', ioe
finally
# clean up code
# take action when no exception occurred:
try
# do some stuff
catch ioe as IOException
print 'I/O error:', ioe
success
print 'Murphy must be on vacation'
# catch and throw:
try
# do some stuff
catch
# clean up code in response to error
throw
Forums
Exception Handling
4 posts
• Page 1 of 1
Exception Handling
- Charles
- Posts: 2515
- Location: Los Angeles, CA
Re: Exception Handling
Hi Chuck
Thank you for all your answers about delegtas and events. I move on now to exception handling.
I have a wish concerning variable scope It would be nice if a variable declared in the try scope is also available in the catch scope but maybe it is so? It is a fundamental design flawn in VB. But they can’t change it now since too muck code would break
Further, The possibility to easily catch errors and find where it realy occired it would be nice if some reflection information were more easily available in Cobra compared with VB. If I had these small examples available when I learned to program VB and .NET, I would have saved a lot of time. Very much time
Best regards
Csaba
Thank you for all your answers about delegtas and events. I move on now to exception handling.
I have a wish concerning variable scope It would be nice if a variable declared in the try scope is also available in the catch scope but maybe it is so? It is a fundamental design flawn in VB. But they can’t change it now since too muck code would break
Further, The possibility to easily catch errors and find where it realy occired it would be nice if some reflection information were more easily available in Cobra compared with VB. If I had these small examples available when I learned to program VB and .NET, I would have saved a lot of time. Very much time
- Code: Select all
def aMethodShowingSomeCobraWishes
Try
# Variable in the try scope should also be in the catch part
# it is not like that in VB – a design miss and too late to fix
var aNumber as float = 5/0
Catch ex As Exception
# Variable from the try scope also avaialble here
anumber = 0
# my generic: object method execption handling in VB
# It would be nice if Cobra had the object
# and object method name more easily available
# This is VB code:
MsgBox("EXCEPTION in " & Me.GetType().Name & "." & _
System.Reflection.MethodBase.GetCurrentMethod().Name & ": " _
& ex.Message, MsgBoxStyle.Exclamation)
#########################
# my generic: moduler method execption handling in VB
# It would be nice if Cobra had the module and module method name more easily available
var modName as String = ‘SomeModuelName’ # Can’t find this in reflection
..........
Catch ex As Exception
# This is VB code:
MsgBox("EXCEPTION in " & modName & "." & _
System.Reflection.MethodBase.GetCurrentMethod().Name & ": " & _
ex.Message, MsgBoxStyle.Exclamation)
Best regards
Csaba
- Csaba
- Posts: 42
Re: Exception Handling
I use the following to get Type/Class and Method names:
catch ex as Exception
#
# current method
sf = System.Diagnostics.StackFrame(0)
method = sf.getMethod
print method.declaringType # Type/Class name (will include namespace)
print method.name # Method Name
#
# calling method
sf = System.Diagnostics.StackFrame(1)
method = sf.getMethod
print method.declaringType
print method.name
- AlGonzalez
- Posts: 13
- Location: Greenville, SC - USA
Re: Exception Handling
Csaba wrote:I have a wish concerning variable scope It would be nice if a variable declared in the try scope is also available in the catch scope but maybe it is so? It is a fundamental design flawn in VB. But they can’t change it now since too muck code would break
Cobra is like Python and unlike C# in that variables are not scoped to their block. So yes, you can do this.
My experience with going back and forth between Python and C# is that implementing methods is much faster and less distracting in Python. However, I then miss C#'s run-time performance, and the use of declarations and typing can speed up refactoring (due to the compiler finding errors as you reorganize the code). Consequently, Cobra is more like C# at a high level and more like Python inside a method.
Csaba wrote:Further, The possibility to easily catch errors and find where it realy occired it would be nice if some reflection information were more easily available in Cobra compared with VB. If I had these small examples available when I learned to program VB and .NET, I would have saved a lot of time. Very much time
Whether in VB or Cobra, can't you just use "exc.stackTrace" to get that? See MSDN System.Exception.StackTrace
Also, try "cobra -d myprog.cobra" to get debugging info as in the filename and line numbers to make that stack trace more useful.
Also, if you can handle a 5-10MB HTML generated report, try "cobra -er -dst myprog.cobra" on a program that is throwing an exception. The -er gives the exception report and the -dst further enhances it to include a "detailed stack trace" which means you get the values of all arguments and locals on every stack frame. Sometimes I really need that info when I'm working on the compiler itself.
You'll note that when you click an object in the report you will jump down in the report to a table for that object with all its properties and their values. Those values may be objects as well which you can click on. Then you can use your browser's Back button to back up.
HTH
- Charles
- Posts: 2515
- Location: Los Angeles, CA
4 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 39 guests