473,396 Members | 1,938 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,396 software developers and data experts.

about try and catch

BC
Hi all,

I have a method that accepts a string as the parameter. This can be a
base64 image string, or just a normal string. Currently to test whether
it's a image string or just a normal string, I used try and catch:

private void MyMethod(string str){
try{
// If not exception is cought, then it is an image string
MemoryStream stream = new MemoryStream(Convert.FromBase64String(str));
Bitmap bitmap = new Bitmap(stream);
...
}
catch{
// If exception is cought, then I assumed it is a normal string
string normalStr = str;
...
}
}

The codes work fine and gave me what I want. However, some while ago an
article I read mentioned that using try and catch could slow down
performance. My question is that if I don't use try and catch to test
the string, what is the alternative ways I can do this? Also, about the
performance thing, how true is it?
Cheers,

Benny

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #1
7 5381
Hello,
Yes using try catch to check the validity of data is not a good idea.
I think you should have a boolean value as a parameter also. to indicate
that if the string is Base64 or a normal string. I prefer to use a code
like this..

public void MyMethod(string paramString,bool bIsBase64String)
{
if(bIsBase64String)
{
MemoryStream stream = new
MemoryStream(Convert.FromBase64String(paramString) );
Bitmap bitmap = new Bitmap(stream);
}
else
{
string theString = paramString; //You don't have to do it.. you
can use the paramString instead ;)
//Your code...
}
}
}

HTH. Cheers :)
Maqsood Ahmed [MCP,C#]
Kolachi Advanced Technologies
http://www.kolachi.net

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #2
BC
Hi Maqsood,

Thanks for the suggestion. However the thing is I am reading the string
from a resource file (XML file) from a third party. Therefore I have no
idea whether the string is an image string or just a normal string
before hand.

Any other ideas?
Cheers,

Benny

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #3
"BC" <no****@devdex.com> wrote in message
news:ev****************@TK2MSFTNGP14.phx.gbl...
The codes work fine and gave me what I want. However, some while ago an
article I read mentioned that using try and catch could slow down
performance. My question is that if I don't use try and catch to test
the string, what is the alternative ways I can do this? Also, about the
performance thing, how true is it?


The only way to not use try catch is to check if it is a base64 string or
not in some way. I'm not sure if that way exists or not but if you can't
find it then all you can do is use try catch. Performance is not too much of
a problem, you can trap several thousand exceptions per second. The
performace becomes a problem if you catch a lot of exceptions in a tight
loop it could make the loop hundreds or thousands of times slower. Depends
how often you are calling your function and how long it takes if it succeeds
I guess.

Michael
Nov 17 '05 #4
BC <no****@devdex.com> wrote:
I have a method that accepts a string as the parameter. This can be a
base64 image string, or just a normal string. Currently to test whether
it's a image string or just a normal string, I used try and catch:

private void MyMethod(string str){
try{
// If not exception is cought, then it is an image string
MemoryStream stream = new MemoryStream(Convert.FromBase64String(str));
Bitmap bitmap = new Bitmap(stream);
...
}
catch{
// If exception is cought, then I assumed it is a normal string
string normalStr = str;
...
}
}

The codes work fine and gave me what I want. However, some while ago an
article I read mentioned that using try and catch could slow down
performance. My question is that if I don't use try and catch to test
the string, what is the alternative ways I can do this? Also, about the
performance thing, how true is it?


While exceptions aren't hugely cheap, they're not nearly as expensive
as they're made out to be. If there were methods to check whether or
not a stream actually contained an image, and whether or not a string
was a valid base 64 string that would be preferrable from a readability
point of view, but unless exceptions are being thrown a *lot*, they're
unlikely to have any significant performance impact.

Of course, you could do a very quick check first - if the length of the
string isn't a multiple of 4, then it *definitely* isn't a base64
string. If you have a lot of non-base64 strings that could make some
difference.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5


"BC" wrote:
I have a method that accepts a string as the parameter. This can be a
base64 image string, or just a normal string. Currently to test whether
it's a image string or just a normal string, I used try and catch:

private void MyMethod(string str){
try{
// If not exception is cought, then it is an image string
MemoryStream stream = new MemoryStream(Convert.FromBase64String(str));
Bitmap bitmap = new Bitmap(stream);
...
}
catch{
// If exception is cought, then I assumed it is a normal string
string normalStr = str;
...
}
}


I'm assuming you have two possibilities:
1) Your string is a base64 encoded stream
2) Your string is a path to an image file

If that's the case, you can use the following code:

private Bitmap MyMethod(string str)
{
if (File.Exists(str))
// we have a file
return new Bitmap(str)

// we have an base64 encoded image
MemoryStream stream = new MemoryStream(Convert.FromBase64String(str));
returm new Bitmap(stream);
}
Nov 17 '05 #6
>
While exceptions aren't hugely cheap, they're not nearly as expensive
as they're made out to be.


I disagree, they can be very expensive, but this aspect of it has been
overhyped - many applications are simply not affected by the performance
hit.

I believe that correct program behavior is more important than squeezing a
few more cycles from a processing path, and that improvements in processor
speed and design, and future changes to the CLR runtime, can speed up
exception handling speeds a great deal.

One reason why exception processing is slow is that AFAIK all exceptions get
routed through the kernel and back to the Win32 subsystem before the CLR
actually starts processing it - processor caches get flushed, ring
transitions occur, etc. - this will always be slow. Perhaps one day the CLR
will check the stack of the faulting thread to determine if it needs to
route it through the kernel (e.g. if part of the thread winds through
unmanaged code), and if not, handle the whole thing locally.

I don't know what changes have been made to this part of the runtime in
Whidbey so I don't know much that aspect of it has changed.

regards,
Dave

Nov 17 '05 #7
On Fri, 29 Apr 2005 03:08:04 -0700, StealthyMark
<St**********@discussions.microsoft.com> wrote:


"BC" wrote:
I have a method that accepts a string as the parameter. This can be a
base64 image string, or just a normal string. Currently to test whether
it's a image string or just a normal string, I used try and catch:

private void MyMethod(string str){
try{
// If not exception is cought, then it is an image string
MemoryStream stream = new MemoryStream(Convert.FromBase64String(str));
Bitmap bitmap = new Bitmap(stream);
...
}
catch{
// If exception is cought, then I assumed it is a normal string
string normalStr = str;
...
}
}


I'm assuming you have two possibilities:
1) Your string is a base64 encoded stream
2) Your string is a path to an image file

If that's the case, you can use the following code:

private Bitmap MyMethod(string str)
{
if (File.Exists(str))
// we have a file
return new Bitmap(str)

// we have an base64 encoded image
MemoryStream stream = new MemoryStream(Convert.FromBase64String(str));
returm new Bitmap(stream);
}

The original poster was worried about his try-catch being too slow. I
would think that calling File.Exists would be a LOT slower than the
try-catch mechanism - it has to go to the disk, maybe multiple times,
and search the directory.
Nov 17 '05 #8

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

Similar topics

3
by: Steve | last post by:
I have some general catch clauses in my app as follows: try { } catch(Exception ex) { } try
5
by: John Richardson | last post by:
Quick question about the UnhandledException event and associated Handler. I just implemented this handler for the first time, and am surprised that it this event is being raised for an exception...
5
by: Patrick Dickey | last post by:
Hello, All! I'm modifying some source code that I downloaded from Planet-source-code a while back, and have a question about Try Catch statements. Basically, I'm wondering if I can do a Try with...
9
by: Gustaf | last post by:
I'm confused about structured error handling. The following piece of code is a simplification of a class library I'm working on. It works, and it does what I want, but I'm still not convinced that...
10
by: tony | last post by:
Hello!! As you know every user defined exception must be derived from class Exception. Now to my question if I write catch then every exception will be caught. If I instead write...
1
by: UJ | last post by:
I am doing development on a machine and everything was working fine. The name of the project was ECS to I made all my references as ~/ECS/... Worked great. Put it on the final server running...
2
by: pack | last post by:
"Unless you have a very good reason to catch an exception, DON'T. Exceptions are supposed to be exceptional, just like the dictionary meaning: uncommon, unusual. When in doubt, let the calling...
34
by: vpriya6 | last post by:
Hi guys, I am new to Ajax, xml and javascript. I want to know how can I retrieve data from xml and display in the html page? please help me out. suppose my xml file is customer.xml the code...
6
by: rhaazy | last post by:
I am looking for some feedback on using try catch statements. Usually when I start a project I use them for everything, but stop using them as often after the "meat n' potatos" of the project is...
9
by: Adem | last post by:
Is it possible to get some info about an unknown exception, ie. the "catch (...)" case below: catch (const blah1 &ex) { cout << "blah1 exception." << endl; } catch (const blah2 &ex) {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.