Results 1 to 6 of 6
- 09-22-2010, 11:05 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 35
- Rep Power
- 0
Why syntax limitation with try/catch?
With reference to if blocks, I notice that you can do this:
... or this:Java Code:if (condition1) method1(); else if (condition2) method2(); else if (condition3) method3(); //etc ...
... and the compiler is happy either way.Java Code:if (condition1) { method1(); } else if (condition2) { method2(); } else if (condition3) { method3(); } //etc ...
But if you try to do this (for example):
the compiler complains and insists that you do this:Java Code:try method(); catch (Exception1 e1) err_method1(e1); catch (Exception2 e2) err_method2(e2); catch (Exception3 e3) err_method3(e3);
Why does it do that with try/catch but not if?Java Code:try { method(); } catch (Exception1 e1) { err_method1(e1); } catch (Exception2 e2) { err_method2(e2); } catch (Exception3 e3) { err_method3(e3); }
- 09-22-2010, 12:00 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Imagine a nested try ... catch( ... ) statement:
Now imagine you don't need the curly brackets (i.e. a Statement instead of a Block syntactically):Java Code:try { try { ... } catch ( ... ) ... } catch ( ... ) ...
You can't distinguish to wich try block the latest catch clause belongs anymore ...Java Code:try try ... catch( ... ) ... catch( ... ) ...
kind regards,
Jos
- 09-22-2010, 12:04 PM #3
Member
- Join Date
- Aug 2010
- Posts
- 35
- Rep Power
- 0
I see, yes it's different to if, I hadn't thought of that! ... but you could distinguish which block if there was an endtry keyword!
- 09-22-2010, 12:06 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
- 09-22-2010, 12:07 PM #5
Member
- Join Date
- Aug 2010
- Posts
- 35
- Rep Power
- 0
I know ... but they should! How dare they ... :p
- 09-22-2010, 03:25 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
try catch help
By vividcooper in forum New To JavaReplies: 8Last Post: 02-11-2010, 09:00 AM -
how to catch two exceptions in one catch()?
By arnab321 in forum New To JavaReplies: 1Last Post: 11-06-2008, 10:54 AM -
try catch...
By MarkWilson in forum New To JavaReplies: 8Last Post: 06-27-2008, 05:39 PM -
try catch!?
By Joe2003 in forum Advanced JavaReplies: 2Last Post: 01-28-2008, 07:51 PM -
Use try and catch
By zoe in forum New To JavaReplies: 2Last Post: 07-25-2007, 07:50 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks