If Throw An Exception From Within A Catch, Does The Finally (from The Catch) Still Execute

Jun 2, 2009

If I throw an exception from within a catch, does the finally (from the catch) still execute? i.e.

Try
..some code with an exception in it...
catch ex as Exception
throw new SpecialException("blah blah" & ex.Message, ex)

[code]....

View 6 Replies


ADVERTISEMENT

Catch / Handle And Then Again Throw Exception?

Jun 26, 2009

I ran into an interesting dilemna today. I have a function that handles information and checks for duplicate values, then returns the next number that is not a duplicate. So, I have something like this:

Public Function GetNextNonDuplicateNumber(NumberToCheck as Long) as Long
//the non-duplicate the function will return
Dim NonDuplicate as Long
'duplicate
If CheckForDuplicate(NumberToCheck) = True Then
Throw New DuplicateException()
[Code] .....

As you can see, I want to handle the exception specifically, but I also want to throw it when I'm done because I want to alert other code outside the function. The problem is that simply throwing it exits out of the function with a null value.

View 2 Replies

C# - Use Case For Try-catch-finally With Both Catch And Finally

Feb 17, 2010

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.

View 10 Replies

Error 68 'Catch' Cannot Catch Type 'Microsoft.Office.Interop.Outlook.Exception' Because It Is Not 'System.Exception'

Feb 12, 2010

I am using VB.NET 2005 to create a Windows forms application. I have a procedure named SendMail that creates an instance of Outlook.Application, to send an email from my application. I found the code on this forum, I think.The procedure works fine, but I can't use error handling with it.I call the procedure from a button click event. I put the procedure call in a try/catch block, and the application won't build, with the following error.

Error 68 'Catch' cannot catch type 'Microsoft.Office.Interop.Outlook.Exception' because it is not 'System.Exception' or a class that inherits from 'System.Exception'. C:datadevdmtiQTSQTSv7_1_20100212wQTSQTSReportsCriteria
pt_frmReportViewer.vb 43 21 QTS

Here is the code:

Sub SendMail(ByVal sFile As String)
' Create an Outlook application.
Dim oApp As Outlook._Application

[code]....

View 8 Replies

Catch An Exception And Throw A New Exception Which Wraps The First Exception As An Inner Exception

May 8, 2009

It is possible to catch an exception and throw a new exception which wraps the first exception as an inner exception: [URL] Also, if I call a function and it throws a certain error, but I catch it, will the calling code's catch handler execute? If so, and it is of a higher exception type, is this not wrapping the exception? eg I can throw exception ex of type IndexOutOfRange, log it but rethrow, catch a higher up exception and do something, or I can throw a new exception and wrap an inner exception like:

View 1 Replies

Button_click Event Will Not Stop Execute After Error Catch Using Try - Catch Statement

Apr 1, 2011

i have problem when i click a ADD button, there is one null value in the textbox .. so the try catch statemnt is to catch that null value error but after that the catch is success but the button click never stop excute the statemnt till the end of the button event.

View 6 Replies

Error - Catch Cannot Catch Type 'Microsoft.Office.Interop.Outlook.Exception'

Mar 25, 2011

I have a program in VB.Net that receives mails from Outlook, extracts attachments and inserts the attachments into a table through a query. I would like to put the query/queries in a Try/Catch block, but cannot do so as Outlook exceptions cannot be caught, and it gives me an error, and unless I put a very specific exception, I cannot catch it. Is there a workaround?

Edit:

Try
Catch ex As Exception
End Try

Exception is underlined and when I hover on it, it says: "Catch cannot catch type 'Microsoft.Office.Interop.Outlook.Exception' because it is not in 'System.Exception' or a class that inherits from 'System.Exception'". This is affecting all my other code which I'd like to put into a Try/Catch block.

View 2 Replies

Nested Try...Catches - If An Exception Occurs In The 2nd Sub's Try...Catch, Which Catch Gets Excecuted?

Mar 4, 2009

I have this scenario: in a Sub I have a Try...Catch statement.Within that Try..Catch I call another sub.In that 2nd sub is also a Try...Catch.(see below for example).Now if an exception occurs in the 2nd sub's Try...Catch, which Catch gets excecuted? The 2nd one, the 1st one or both?

Private Sub sub1()
Try
..do stuff[code].....

View 3 Replies

Catch' Cannot Catch Type 'Object' Because It Is Not 'System.Exception' Or A Class That Inherits From 'System.Exception'

Aug 10, 2010

I'm getting 'Catch' cannot catch type 'Object' because it is not 'System.Exception' or a class that inherits from 'System.Exception'. and 'Expression detected' Code underlined in blue: Catch obj1 As Object When (?)

Private Sub OpenJAMem()
Dim num3 As Integer
Try

[code]....

View 2 Replies

'Catch' Cannot Catch Type 'exception' Because It Is Not 'System.Exception'?

Apr 16, 2012

I have a project converted from Visual Studio 2005 to Visual Studio 2010. It compiles and runs.

When I prune "unused references's from this project as listed by Visual Studio, the project fails to compile with the subject error message.

In particular one of the "unused refererences" is to "Microsoft.VisualBasic.dll". The IDE does not allow me to add back this particular reference.

What do I need to add back as a referenceto get this items?

View 4 Replies

Why Use Finally In Try/Catch

Nov 25, 2009

OK, I'd like to know the difference between the following two blocks of code:

Block A
Try
Something

[code].....

View 3 Replies

C# :: Why Can't Write Just A Try With No Catch Or Finally

Dec 27, 2010

Sometimes I do this and I've seen others doing it too:

VB:
Try
DontWannaCatchIt()

[code].....

View 10 Replies

Catch Exceptions In Finally Block?

Oct 13, 2010

Is there some special way to catch exceptions in finally block ?

View 2 Replies

Return Inside Try/catch/Finally?

Sep 15, 2009

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?

[Code]...

View 7 Replies

Try Catch Finally Doesn't Work?

Mar 23, 2010

I've a class with a timer inside

Private tt As New System.Timers.Timer()
This timer is enabled in the New() of the class
Try

[code].....

View 10 Replies

VS 2008 How To Use Try Catch Finally Statement

Jun 15, 2009

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'.

View 4 Replies

VS 2008 How To Use Try..Catch...Finally Statement

Mar 4, 2011

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

[code]....

View 14 Replies

Which Is Faster - A Using Block Or Try / Catch / Finally

Sep 2, 2010

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:

[Code]...

View 4 Replies

.net - Exception Handling: Is Finally Executed After Throw?

Oct 16, 2009

Assume you have the following code:

Instead of doing:
Try
'
[code].....

View 5 Replies

.net - Replicate Try/catch/finally Using On Error Goto

Jun 14, 2012

Suppose in VB.NET you have:

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).

View 2 Replies

Trap Any Exceptions With Try Catch Finally If The Reader?

Apr 8, 2011

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.

View 2 Replies

.net - Try Catch Finally Blocks.. Do I Still Need Them When Handling Errors In The Global.asax

Jun 2, 2010

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?

View 1 Replies

Is Try / Catch / Finally Required With Using Statement In Order To Handle Exceptions

Jan 31, 2012

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]

View 3 Replies

Thread Safe To Throw And Catch Shared Exceptions?

Jan 25, 2010

so i have a Method that is going to made Thread Safe. can i have something like this in the Method:

Public Class Q Private Shared ASD As New MyException("") Public Sub W Throw ASD if multiple threads attempt to throw the Shared exception ASD, will there be an error in the catching part? The alternative of course is to: Throw New ASD but i'm just checking to see if the first way is thread safe

View 4 Replies

Restructure Try / Catch To Eliminate Error On Myreader.close Command In Catch Part?

Jun 19, 2012

The following code causes a "Warning" that Variable is used before value assigned.How do I restructure this Try/catch to eliminate error on the myreader.close command in the Catch part? Code appears to work fine but I dont like Warnings. [code]

View 8 Replies

Catch Log4net Exceptions / Use Try / Catch Approach

Jan 23, 2012

I need to catch log4net exceptions (its own exceptions not app exceptions logged by it). I wish there's a way of doing it this way: [code] I have this code implemented and there's no errors in compilation but i force log4net to have an error (pointing to a non existing database in the config file) and nothing is threw.I've tried the listener aproach: [code] and it's writing the errors to log4net.txt, the forced ones i mean.This last aproach has a couple of drawbacks: it won't append every error to the file, if the error is the same it doesn't write it, i can't get the listener to write every error to that file, only one (I don't know how to fully configure the trace listener, it might be that). Thus it won't append the date and hour to every line wich is a necesity for me. Finally i can't give structure to it (xml). Even if the listener work i need to use the try/catch aproach, since i'm using ExceptionHandling from Enterprise library to log the errors in my app.

View 1 Replies

Catch Inner Exception In NET?

Oct 13, 2009

How can I catch an inner exception in .NET? I need to check 2 databases for a record. The database code throws an exception if a record is not found, so then I want to check the second database[code]...

View 5 Replies

How To Catch Exception From DLL

Sep 8, 2011

I've written a DLL that contains a bunch of classes. One of my methods validates the contents of a file and throws any necessary exceptions based on invalid content - I'm using a standard "throw new exception." call the thing is, when my code reaches the line where the exception is thrown, my Visual Studio IDE hangs up on that line instead of the external call to my validate method.

[Code]...

So my IDE is actually pulling up the VB file that contains the DLL content and highlights the line in the ValidateFile instead of just hanging on the oFC.ValidateFile method line where the exception is thrown instead of the IDE hanging on the oFC.ValidateFile line in my calling application.

I just want user to have access to the any exceptions that are raised. Here if "C:FileThatDoesntExist.txt" file does not exist then Dll throw exception which should be catch through Application.ThreadException.. How to Catch exception from DLL ?

View 3 Replies

How To Catch This Exception

May 1, 2012

System.Data.OleDbException (0x80004005): The changes you requested to the table were not successful because they would create duplicate values in the index, primary key..."I know how to catch all exceptions. But I want to catch THIS SPECIFIC ONE so I can call one of my subroutines to generate a different ID that will not violate the primary key.

View 2 Replies

.net - Log With Asp.net HealthMonitoring An Exception That Was Catch?

Sep 2, 2011

I configured HealthMonitoring in my web application to log all exceptions in the database.But, when I gracefully handle some exception by showing a nice message to the user, they are not logged in the database.If I something like Throw ex, the standard error page is be displayed and the error is not gracefully handled.

Is there something else I could add at the end of my error handler to tell the system to also log this error?here an code sample:Dim errorText As String = "Doing Something"Try 'Do Something

[Code]...

View 2 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved