473,388 Members | 1,198 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

[C#] Is it possible to implicitly wrap each line in a try/catch block?

Logan1337
Hi all. I have a method involving several statements, none of which are dependent on any other. Basically I am simply setting a bunch of properties, one after the other, and want to essentially ignore exceptions on a per-line basis throughout this block. In other words, if an exception occurs, I only want that line to be skipped and execution continue on the next, just as if I had this:

Expand|Select|Wrap|Line Numbers
  1. void method() {
  2.     try { NonCriticalOperation(); } catch { }
  3.     try { NonCriticalOperation(); } catch { }
  4.     try { NonCriticalOperation(); } catch { }
  5.     try { NonCriticalOperation(); } catch { }
  6.     try { NonCriticalOperation(); } catch { }
  7. }
...but without having to write a million try/catch statements.

Anyone know of an attribute or keyword that can do this?
Aug 21 '07 #1
3 1996
Shashi Sadasivan
1,435 Expert 1GB
Hi all. I have a method involving several statements, none of which are dependent on any other. Basically I am simply setting a bunch of properties, one after the other, and want to essentially ignore exceptions on a per-line basis throughout this block. In other words, if an exception occurs, I only want that line to be skipped and execution continue on the next, just as if I had this:

Expand|Select|Wrap|Line Numbers
  1. void method() {
  2.     try { NonCriticalOperation(); } catch { }
  3.     try { NonCriticalOperation(); } catch { }
  4.     try { NonCriticalOperation(); } catch { }
  5.     try { NonCriticalOperation(); } catch { }
  6.     try { NonCriticalOperation(); } catch { }
  7. }
...but without having to write a million try/catch statements.

Anyone know of an attribute or keyword that can do this?
I have no clue about that.
But if you can write custom snippets it will help (or try editing the try snippet and save it as a new one)
Aug 22 '07 #2
Plater
7,872 Expert 4TB
I don't believe it's possible...UNLESS you can manage to do something like a loop for property setting

Psuedo:
Expand|Select|Wrap|Line Numbers
  1. foreach(property myprop in PropertiesToSet)
  2. {
  3.    try
  4.    {
  5.       SomeFunctionToSetProperty(myprop);
  6.    }
  7.    catch(Exception e)
  8.    {
  9.       //ignore errors
  10.    }
  11. }
  12.  
Aug 22 '07 #3
TRScheel
638 Expert 512MB
You may want to read:

Why Doesnt C# Have Exception Specifications?


On a side note, its generally bad practice to just ignore exceptions. Granted, there are times when its warranted, but I would seriously consider if there was another way to go about it.
Aug 22 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: B.J. | last post by:
Hi, Is there any way how I can shorten following code ? try {...} catch(FormatException) {SAME CODE IN ALL CATCH BLOCKS} catch(OverflowException) {SAME CODE IN ALL CATCH BLOCKS} E.g. to...
3
by: Travis | last post by:
I am trying to prevent an exception from bubbling up from one nested Try/Catch Block to its "Parent" Try/Catch Block. Here is some example code: try{ try{ //Non-application killing code that...
18
by: Simon | last post by:
I was of the impression that code placed after a Try...Catch block was only executed if there was no exception thrown. I've got some VB.net code as part of a Windows form that executes even...
1
by: Solution Seeker | last post by:
Hi, I have a Problem when i try to put a Try Catch Block Outside all the Functions in a Class. But in .Net, it throws an error. I like to catch all the Unhandled Exception that occured inside...
4
by: GS | last post by:
Hi, Is it possible to use try-catch block to return back to processing on the same line where last error occured? Say I'm trying to establish connection to database in try {} and catching...
2
by: davidz | last post by:
When a catchable error occurs in a try block, control is passed to the associated catch block. While I am then able to examine properties of the error using such functions as ERROR_NUMBER() in the...
6
by: foolmelon | last post by:
If a childThread is in the middle of a catch block and handling an exception caught, the main thread calls childThread.Abort(). At that time a ThreadAbortException is thrown in the childThread. ...
10
by: Curious | last post by:
Hi, I've found that if I wrap my code with a try-catch block in the fashion illustrated below, it'll do what is specified in the catch section instead of crashing. Could anyone confirm that this...
8
by: =?Utf-8?B?U2F2dm91bGlkaXMgSW9yZGFuaXM=?= | last post by:
Is it right when placing the RETURN statement inside the TRY or inside the CATCH statement, when there is a FINALLY clause? Especially when there is a transaction going on, in the try/catch block?...
16
by: HillBilly | last post by:
This is freaking me out. I'm using Membership and trying to determine if the database is online. The GetConnectionString( ) method returns a connection string as expected but not when used in the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.