Click here to Skip to main content
15,886,078 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Which will be Beneficial?

If-Else Or Try-Catch?

Provide comparisons please.
Posted
Updated 9-Apr-11 3:17am
v2
Comments
Dalek Dave 9-Apr-11 9:18am    
Edited for Spelling (Beneficial, not Benefitial).
(Don't worry, easily done).
Toli Cuturicu 12-Apr-11 4:48am    
What is better? An Elephant or a Soda Vending Machine...?

Both are very different things really and cannot be compared.
What exactly are your trying to do?

IMO, you should have a look at msdn or a good book and try and understand what both these contructs really do.
 
Share this answer
 
Comments
Albin Abel 9-Apr-11 13:27pm    
Agreed both are different things
Abhinav S 9-Apr-11 13:54pm    
Thanks.
If-else is a conditional clause while try catch is for error catching and handling. So they are very different things and thus not comparable. For more info see:
- http://msdn.microsoft.com/en-us/library/5011f09h(v=VS.100).aspx[^]
-http://msdn.microsoft.com/en-us/library/0yd65esw(v=VS.100).aspx[^]
 
Share this answer
 
Comments
Albin Abel 9-Apr-11 13:28pm    
Good links and agreed both are different things. My 5
It is like comparing Apples and Bananas.

They are completely different things.

If/Else is effectively a true or false comparison of a condition.

Pseudo Code:

If (Something) is true Do Action#1
Else Do Action#2

For Example:
C#
if (UserInput.Name = "David")
{
    MessageBox.Show("Good Morning Dave");
}
else
{
    MessageBox.Show("I am Sorry, I can't do that.");
}




Try/Catch is a filter that stops things that are not already catered for.
(ie It will catch all things that do not have exceptions).

Pseudo Code:

Try [This Thing]
If [This Thing] OK Then Do Something
If [This Thing] Is unknown to the System, Go to Catch.

Catch [This Thing] Do something else


For Example
C#
try
{
rtb.LoadFile("C:/test.txt");
}
catch (System.Exception excep)
{
MessageBox.Show(excep.Message);
}



If Helpful Please Vote Up
 
Share this answer
 
v3
Comments
Albin Abel 9-Apr-11 13:27pm    
well explained. my 5
This is to summarize our expert's answers. If else is decision and branching logic. When you have a determined logic if else is useful.

If there is an undetermined condition, (just a guess possible) then error handling would be needed. Let the exception throw only on unexpected situation, not due to a bad coding. Throwing exception has little over heads. Also never use a general try catch block, use specific catch. otherwise the caller will not be knowing there is a problem. if exception can't be handled by a logic , don't stop it let it flow through the stack and notify it.

As others said both have different purpose, can't one replace the other.
 
Share this answer
 
Comments
Abhinav S 9-Apr-11 13:54pm    
Good answer. 5.
durgeshtupkar10 11-Apr-11 2:40am    
actually in my coding if I go with Try-Catch I can use the particular code many times ,
but it's complex for making of that code.
And if I use If-else the code is easy but gets longer.
Then which way i must use?
both are logically same..
but i prefer TRY-CATCH
 
Share this answer
 
Comments
Toli Cuturicu 12-Apr-11 4:48am    
Joke, right?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900