try-catch

try... except... except... : how to avoid repeating code

I'd like to avoid writting errorCount += 1 in more than one place. I'm looking for a better way than success = False try: ... else: success = True finally: if success: storage.store.commit() else: storage.store.rollback() I'm trying to avoid store.rollback() i...

Why catch and rethrow Exception in C#?

Hi, Folks, forgive me, I'm pretty much a raw prawn when it comes to C#, and .NET generally... though I've been a professional programmer for 10 years. I'm looking at this article: http://www.codeproject.com/KB/cs/datatransferobject.aspx on Serializable DTO's. The article includes this piece of code: public static string SerializeDTO(...

Is there a better way to get visual studio to ignore try/catch in debug mode

I want the designer to catch the error when I am debugging and I want the user to see my friendly message if an error occurs for them. I know I can acomplish this with the following: #If Debug=False Then Try #End If 'some code here #If Debug=False Then Catch ex as exception Messagebox.Show("Errors suck") End Try #End If I...

TRY CATCH on a CONVERT in a Select Statement

Is it possible to use TRY CATCH blocks in SQL Selects? For stuff similar to this for example: select order, CONVERT(DATETIME, orderDate) from orders What's the best way of handling this scenario? ...

Try catch statements in PHP

Hey everyone, Im quite new to programming so please be nice :) I am currently experimenting with try-catch statements, I read the documentation on the php.net website and didn't find it all that helpful. I understand what they do, but from reading it, i would not be able to implement one into my own code. I need a real example to help m...

Handling exceptions within custom Functions

All of us use the try catch blocks. But what is the best way of handling errors within custom functions? Display a messagebox to the user when the exception is thrown (if possible within the function), or return a value indicating that an error was found within the function? ...

Curious C# using statement expansion

I've run ildasm to find that this: using(Simple simp = new Simple()) { Console.WriteLine("here"); } generates IL code that is equivalent to this: Simple simp = new Simple(); try { Console.WriteLine("here"); } finally { if(simp != null) { simp.Dispose(); ...

Usage of try/catch blocks in C++

In general, I tend to use try/catch for code which has multiple failure points for which the failures have a common handler. In my experience, this is typically code which qualifies input or context before performing some action or output after performing some action. I have received counsel from literature and colleagues to minimize...

How to skip a record in a Foreach

Hello, I am trying to create an object from an Active Directory base on a simple login. The problem is that some of the login information is valid. How could I just use a try-catch so that if an exception is thrown, just skip to the next login? Here is the code: foreach (var PharosUserItem in ListRef) { ADUser User; try {...

How to surround code with try/catch most elegantly

I often have some problems when using try-and-catch: 1) Some variables need to be declared inside the try brackets otherwise they will not be in scope 2) Ultimately even my return statement ends up having to be in the try bracket but then the method doesn't return anything. What is the proper way to get around this sort of problem. A...

Successful Strategies for TRY ... CATCH in SQL Server 2005

I'm changing some code to take advantage of TRY ... CATCH in SQL Server 2005. What successful strategies have you found for using it? I'm thinking of creating a stored proc which calls the system functions which give details of the error, rolls back any open transaction and raises an error. Is there a better way? ...

Why does resharper say 'Catch clause with single 'throw' statement is redundant'?

I thought throwing an exception is good practice to let it bubble back up to the UI or somewhere where you log the exception and notify the user about it. Why does resharper say it is redundant? try { File.Open("FileNotFound.txt", FileMode.Open); } catch { throw; } ...

Catch, Handle, then Rethrow Exception?

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

Javascript if typeof ='undefined' in try/catch space

I have code that is wrapped in try/catch block. I use typeof to find out if a variable is defined: if (typeof (var) == 'string') { //the string is defined } However, using this in a try/catch block, jumps to the catch part instead of doing what it is suppoed to do (do something with the string if its defined). How can I check i...

PHP try-catch blocks: are they able to catch invalid arg types?

Background: Suppose I have the following obviously-incorrect PHP: try{ $vtest = ''; print(array_pop($vtest)); }catch(Exception $exx){} For it to work with array_pop, $vtest should obviously be an array, not a string. Nevertheless, when I run this code the Warning is exhibited. I don't want that, I just want the...

Does the .NET JIT optimize nested try/catch statements?

Hello: I've been thinking about nested try/catch statements and started to think about under which conditions, if any, the JIT can perform an optimization or simplification of the compiled IL. To illustrate, consider the following functionally-equivalent representations of an exception handler. // Nested try/catch try { try { ...

Throw Exception VS Return Error within a Try,Catch,Finally

I'm pretty sure I already know the answer, but I'm still curious what the opinion is on handling an error within a Try,Catch,Finally block -- but when you're repeating yourself. BTW - I'm not talking about User Input - but using that for an example because it is clear and short Consider this bit of code... try { if (success) {...

Convert String to Double - VB

Is there an efficient method in VB to check if a string can be converted to a double? I'm currently doing this by trying to convert the string to a double and then seeing if it throws an exception. But this seems to be slowing down my application. Try ' if number then format it. current = CDbl(x) current = Math.Round(curren...

Try Catch Block in Java

So I'm given this code and I have to create an Exception and then use a Try Catch Block to catch it. I've already made the Exception, at the bottom of the code. But I've never used a Try Catch Block before and am not sure how to implement it. The Exception is if a rank that isn't listed under the enum is entered. I need to use a toStri...

Destructor that calls a function that can throw exception in C++

I know that I shouldn't throw exceptions from a destructor. If my destructor calls a function that can throw an exception, is it OK if I catch it in the destructor and don't throw it further? Or can it cause abort anyway and I shouldn't call such functions from a destructor at all? ...