try-catch

PHP5: Why is try/catch failing?

On my XAMPP/Win XP build, PHP5.2.3 fails to catch any exceptions. None of the examples work, and this: try { throw new Exception('Fail'); } catch (Exception $e) { echo 'Succeed'; } ...results in: Fatal error: Uncaught exception 'Exception' with message 'Fail' in M:\path\to\test.php:4 Stack trace: #0 {main} thrown in ...

Nested try statements in python?

Is there a nicer way of doing the following: try: a.method1() except AttributeError: try: a.method2() except AttributeError: try: a.method3() except AttributeError: raise It looks pretty nasty and I'd rather not do: if hasattr(a, 'method1'): a.method1() else if hasattr(a...

try-catch not working?

I'm using a try-catch block in the following Actionscript 3 code: try { this._subtitle = new SubtitleController(subtitlePath, _framerate); this._subtitle.addEventListener(Event.COMPLETE, subtitleLoaded); } catch (e:Error) { trace('subtitle not found'); } The SubtitleController constructor then tries to load the subtitlePat...

The difference between re-throwing parameter-less catch and not doing anything?

Suppose I have the following two classes in two different assemblies: //in assembly A public class TypeA { // Constructor omitted public void MethodA { try { //do something } catch { throw; } } } //in assembly B public class TypeB { public void MethodB { try { TypeA a = ne...

In what circumstances is @finally non-redundant in Cocoa's try/catch/finally Exception Handling?

Consider the following Cocoa/Obj-C code snippets: MyClass *obj; @try { [obj doSomething]; } @catch (NSException * e) { NSLog(@"Exception occurred: %@", [e description]); } @finally { [obj cleanUp]; } and MyClass *obj; @try { [obj doSomething]; } @catch (NSException * e) { NSLog(@"Exception occurred: %@", [e description]); } [obj...

Best way to implement try catch in php4

What is the closest you can get to a try-catch block in php4? I'm in the middle of a callback during an xmlrpc request and it's required to return a specifically structured array no matter what. I have to error check all accesses to external resources, resulting in a deep stack of nested if-else blocks, ugly. ...

Thoughts on try-catch blocks

What are your thoughts on code that looks like this: public void doSomething() { try { // actual code goes here } catch (Exception ex) { throw; } } The problem I see is the actual error is not handled, just throwing the exception in a different place. I find it more difficult to debug because i...

Why is my NullPointerException not being caught in my catch block?

I have a thread in which I catch all errors in a big, all-encompassing catch block. I do this so that I can report any error, not just expected ones, in my application. My Runnable looks like this: public final void run() { try { System.out.println("Do things"); /* [1] */ doUnsafeThings(); } catch (Throw...

Whether should I use try{} or using() in C# ?

Hello, I'm new to C#,been a pascal lover until I found C# in Depth.In Delphi there is a try{} statement that is also implemented in C#. However,I've seen some of you mentioning "Using(){} is better than try{}". Here's an example: //This might throw an exception sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, Pr...

Try/Catch and threading

I have an idea why but I'd like to ask if someone has a good grasp on why the exception raised inside a thread is never caught by the code that started it. Here's some very simple code to demonstrate what I mean: using System; using System.Collections.Generic; using System.Threading; namespace TestCrash { class Program { ...

How can I handle lost client connection in a SQL Server T-SQL try-catch block?

The TSQL BEGIN TRY and BEGIN CATCH block pattern does not catch errors due to a lost client connection. How can I catch and handle a lost client connection? I'm setting a flag that indicates processing while processing a loop of individual transactions and the catch block re-sets that flag on error, but if the client connection is lo...

Handing exception in BLL and return to client (either winforms or webforms)?

Hi I am looking for the best way to do exception handling, for example.. when an error occurs in the business logic layer, is the best way to stop the METHOD using a catch and return an EVENT to the presentation layer? What should this event contain? Or should i always BUBBLE up exceptions and handle them in the presentation layer? ...

In delphi 7, is `try ... except raise; end;` meaningful at all?

In some Delphi 7 code I am maintaining, I've noticed a lot of instances of the following: with ADOQuery1 do begin // .. fill out sql.text, etc try execSQL; except raise; end; end; It seems to me that these try blocks could be removed, since they do nothing. However, I am wary of possible subtle side-effects.. Can any...

problem in try statement

Hello, This is the code I use to setup my TCP server internal void Initialize(int port,string IP) { IPEndPoint _Point = new IPEndPoint(IPAddress.Parse(IP), port); Socket _Accpt = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { _Accpt.Bind(_Point); ...

try-catch SQLite .NET: open connection

I have the following method inside class DBConnection. I call the method like this: SQLiteConnection conn = DBConnection.OpenDB(); when I want to open an connection, so that I can execute my queries. I can call a similar method when I want to close the connection. The method: public static SQLiteConnection OpenDB() { try { ...

Catch only some runtime errors in Python

I'm importing a module which raises the following error in some conditions: RuntimeError: pyparted requires root access I know that I can just check for root access before the import, but I'd like to know how to catch this spesific kind of error via a try/except statement for future reference. Is there any way to differentiate betwe...

Throw-catch cause linkage errors

I'm getting linkage errors of the following type: Festival.obj : error LNK2019: unresolved external symbol "public: void __thiscall Tree::add(class Price &)" (?add@?$Tree@VPrice@@@@QAEXAAVPrice@@@Z) referenced in function __catch$?AddBand@Festival@@QAE?AW4StatusType@@HHH@Z$0 I used to think it has to do with try-catch me...

Learning about Try-Catch blocks

I'm a Java beginner so please bear with me :P static int load = 100; static int greet; public void loadDeduct(int cLoad, int c){ int balance; balance = cLoad - 7; System.out.println("Your balance: " + balance); } public void loadDeduct(int tLoad){ int balance; balance = tLoad - 1; System.out.println("Your balan...

How to avoid setting variable in a try statement

My problem is that I have to set a variable in a try statement otherwise I get a compile error. Later on I need to use that variable but it is now out of scope, or so I believe. I initialise the variable outside the try statement and set it to null, I thought that it might then be accessible outside, but I still get a NullPointerExcepti...

What is the purpose of using try, catch blocks?

Is it a replacement for if, then blocks? I have seen a lot of code where they are used like that. ...