I often use the Try block to catch exeptions, and I have read in the help pages to do with the Finally statement. But, from what I understand it has no use whatsoever. Why would I use Finally instead of just ending the Try block. For example, from what I know
Dim o as SomeClass Try o = new SomeClass 'call some method on o here Catch(...)
Why is there a need to set o to Nothing? What if i don't set it to Nothing in the Finally block? What i think is that it is OK if you don't set it to Nothing because the object will be marked for GC.
This is a followup question to this Should I stick with the Try/Catch/Finally construct, or go with the Using construct? Sample Code for Try/Catch/Finally:
Well, I've read (and learned) that the finally block doesn't always execute its code (even apart from pulling the plug).FYI For more information, see try catch finally question
In Finally Block, How can we check if an exception occurred or Not.I am programming in VB.Net. Why i am asking is that in the Finally Block there could be some things you would like to do if an Exception has occured and some things you want to do if an exception has not occured. So, within finally block how do we know if an exception has occured
I understand how try-catch works and how try-finally works, but I find myself using those (usually) in two completely different scenarios:
try-finally (or using in C# and VB) is mostly used around some medium-sized code block that uses some resource that needs to be disposed properly. try-catch is mostly used either
around a single statement that can fail in a very specific way or (as a catch-all) at a very high level of the application, usually directly below some user interface action.
In my experience, cases where a try-catch-finally would be appropriate, i.e., where the block in which I want to catch some particular exception is exactly the same block in which I use some disposable resource, are extremely rare. Yet, the language designers of C#, VB and Java seem to consider this to be a highly common scenario; the VB designers even think about adding catch to using.
Or am I just overly pedantic with my restrictive use of try-catch?
EDIT: To clarify: My code usually looks like this (functions unrolled for clarity):
Try do something Aquire Resource (e.g. get DB connection) Try
[Code]....
which just seems to make much more sense than putting any of the two catches on the same level as finally just to avoid indentation.
I have a (begginner) question on VB.NET. Is this good code (I mean, is it "bullet proof")?. It seems to work, but I understand the Return is executed AFTER the Finally. So how does it call a method on an allready disposed of object?
i want to know how to use try catch finally statement.i want to catch this error Violation of PRIMARY KEY constraint 'XPKemployee'. Cannot insert duplicate key in object 'dbo.employee'.
i have this problem about the try,catch,finally statement... i don't know how to use it..i have my converter program here and this should be type by a letter 'cause if you do it will go lag or hang...i try VERIFY button to make sure if it's a letter or number but my teacher said it's not counted...
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If ComboBox1.Text = "Pounds To Kilo" Then
Try Debug.Print("Trying...") Catch ex as Exception throw new Exception("Exception", ex) Finally Debug.Print("Finally...") End Try
How would you write this using the "On Error Goto" construct? (please no questions asking why I would want to do this, just curious if it can be done).
I have a SqlDataReader and I want to trap any exceptions with Try Catch Finally if the reader =cmd.ExecuteReader() statement fails. If this statement throws an exception do I need to close the reader? If so, where. Wherever I place the reader.close() statement I get an error that the reader has not been assigned a value yet.
I am handling errors via my global.asax in this method:
Dim CurrentException As Exception CurrentException = Server.GetLastError() Dim LogFilePath As String = Server.MapPath("~/Error/" & DateTime.Now.ToString("dd-MM-yy.HH.mm") & ".txt") Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter(LogFilePath)
[code]....
In my code I currently have no other error handling. Should I still insert try, catch, finally blocks?
I wonder how to use statement handles exceptions?Do I need to wrap the using statements with a Try/Cath/Finally clause in order to be sure that the SqlConnection object is closed and disposed even if the containing code throws an exception? [code]
I am looking for a way to block registry changes unless I choose to allow them to be made. So basically if an application tries to make a change my program would display a dialog with information of the change and I can either click Allow Change or Disallow Change.
Only thing is I am not sure how I go about stopping a registry change before it happens.
I have this block of php that i want to convert to c# or vbnet. I am stuck on the array methods since there is no direct equal to the is array and other bits like array merge. I know that arrays behave differently in php than in c# but thats about it. I am looking online but have not found anything yet.
private static function processArgs( $arguments ) { $args = array(); foreach ( $arguments as $arg ) {