473,587 Members | 2,316 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to shorten catch block

Hi,

Is there any way how I can shorten following code ?

try {...}
catch(FormatExc eption) {SAME CODE IN ALL CATCH BLOCKS}
catch(OverflowE xception) {SAME CODE IN ALL CATCH BLOCKS}

E.g. to this ...

try {...}
catch(FormatExc eption | OverflowExcepti on) {...}

Because I need write same code to all catch blocks ...

Thanks, B.J.
Jul 22 '05 #1
4 1506
Please use Generic exception class i.e System.Exceptio n. This is the superset
of all exception classes the u've mentioned.

"B.J." wrote:
Hi,

Is there any way how I can shorten following code ?

try {...}
catch(FormatExc eption) {SAME CODE IN ALL CATCH BLOCKS}
catch(OverflowE xception) {SAME CODE IN ALL CATCH BLOCKS}

E.g. to this ...

try {...}
catch(FormatExc eption | OverflowExcepti on) {...}

Because I need write same code to all catch blocks ...

Thanks, B.J.

Jul 22 '05 #2
You could try this:

try{..}
catch(Exception exc){
treatException( exc);
}

public void treatException( Exception exc){
SAME CODE IN ALL CATCH BLOCKS.
}

In case you want to write different code for FormatException or
OverflowExcepti on you can write that in treatException:

if(exc is FormatException ){CUSTOM FormatException CODE}

or a better one:

switch(exc.GetT ype().ToString( ))
{
case "FormatExceptio n":
CUSTOM FormatException CODE
break;
}

Hope it helps,

Jul 22 '05 #3
Ravi <Ra**@discussio ns.microsoft.co m> wrote:
Please use Generic exception class i.e System.Exceptio n. This is the superset
of all exception classes the u've mentioned.


It's also the superset of many other exceptions though. It's usually a
good idea to only catch specific exceptions unless you *really* want to
catch and handle/log every exception.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 22 '05 #4
You can use double.TryParse () which returns a bool as an out parameter
saying wheather the parse succeeded or not, not exception will be thrown in
any case.

Currently it is not possible to catch multiple exceptions in one catch
block, this feature is called exception filtering and I hope that MS will
include it in one of the next versions of c#, currently it is only available
to vb programmers.

"B.J." <BJ@discussions .microsoft.com> schrieb im Newsbeitrag
news:20******** *************** ***********@mic rosoft.com...
Hi,

Is there any way how I can shorten following code ?

try {...}
catch(FormatExc eption) {SAME CODE IN ALL CATCH BLOCKS}
catch(OverflowE xception) {SAME CODE IN ALL CATCH BLOCKS}

E.g. to this ...

try {...}
catch(FormatExc eption | OverflowExcepti on) {...}

Because I need write same code to all catch blocks ...

Thanks, B.J.

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3659
by: ben | last post by:
i have a bit of code, that works absolutely fine as is, but seems over complicated/long winded. is there anyway to shorten/simplify it? the code is below. description of it: it's like strcpy in that it copies one block of data to another block of data until the block that is being copied contains a zero/null. the difference with this code is...
8
2726
by: Z D | last post by:
Hi, I was wondering what's the point of "finally" is in a try..catch..finally block? Isn't it the same to put the code that would be in the "finally" section right after the try/catch block? (ie, forget the finally block and just end the try/catch and put the code after the try/catch block). Or does the "finally" construct add some...
11
3471
by: Pohihihi | last post by:
I was wondering what is the ill effect of using try catch in the code, both nested and simple big one. e.g. try { \\ whole app code goes here } catch (Exception ee) {}
23
3056
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally block. But, I prefer to dim them "on the fly" only if needed (save as much resources as possible). A little further... I may wish to create a sqlcommand...
4
324
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 this ...
6
7436
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. The question is: after the ThreadAbortException is thrown, does the childThread continue run the remaining code in the catch block?
7
1696
by: K Viltersten | last post by:
I'm using the code below. I know for sure that the data obtained will be a single value. Hence, it feels somewhat like nuking a fly to use the adapter etc. Is it possible to shorten the code for this specific, special case? How? string commandString = "select sth from smwhr where id = 5"; SqlConnection connection
0
8205
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8339
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7967
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.