Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


if statement or try catch block ?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Freiza
Grandmaster Cheater
Reputation: 22

Joined: 28 Jun 2010
Posts: 662

PostPosted: Thu Apr 14, 2011 3:47 am    Post subject: if statement or try catch block ? Reply with quote

Why we don't use simple if statement instead of Try catch block.. in c++ ?
Back to top
View user's profile Send private message Send e-mail
Dark Byte
Site Admin
Reputation: 470

Joined: 09 May 2003
Posts: 25778
Location: The netherlands

PostPosted: Thu Apr 14, 2011 6:18 am    Post subject: Reply with quote

it's faster, if there are more than 2 states to compare against
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
HomerSexual
Grandmaster Cheater Supreme
Reputation: 5

Joined: 03 Feb 2007
Posts: 1657

PostPosted: Thu Apr 14, 2011 6:59 am    Post subject: Reply with quote

You can run code inside an if statement that will crash your program and make it end right there, the try catch block prevents this from happening
_________________
Back to top
View user's profile Send private message
Freiza
Grandmaster Cheater
Reputation: 22

Joined: 28 Jun 2010
Posts: 662

PostPosted: Thu Apr 14, 2011 7:24 am    Post subject: Reply with quote

@HomerSexual
Code:
#include <iostream>
using namespace std;
int main()
{
    int a,b,c=9;
    cin>>a>>b; //b=0;
    try{
        if(b==0)
        throw a;
        c=a/b;
    }
    catch(...)
    {
       cout<<"error";
    }
    cout<<c;
    return 0;
}

if we also have to use if statement like this.. then what is the point of using try-catch block. Instead better use if alone..? Am I wrong..
In C# and Java using exception handling makes sense as we needn't write if statements.


DarkByte wrote:
it's faster, if there are more than 2 states to compare against

I was also thinking the same.. but is it sufficient to make a language feature for this..?
Back to top
View user's profile Send private message Send e-mail
callmenuge
Cheater
Reputation: 0

Joined: 12 Feb 2011
Posts: 42

PostPosted: Thu Apr 14, 2011 4:07 pm    Post subject: Reply with quote

Freiza wrote:
@HomerSexual
Code:
#include <iostream>
using namespace std;
int main()
{
    int a,b,c=9;
    cin>>a>>b; //b=0;
    try{
        if(b==0)
        throw a;
        c=a/b;
    }
    catch(...)
    {
       cout<<"error";
    }
    cout<<c;
    return 0;
}

if we also have to use if statement like this.. then what is the point of using try-catch block. Instead better use if alone..? Am I wrong..
In C# and Java using exception handling makes sense as we needn't write if statements.


DarkByte wrote:
it's faster, if there are more than 2 states to compare against

I was also thinking the same.. but is it sufficient to make a language feature for this..?


There's multiple ways to accomplish an objective. The "try" statement can be an elegant solution to certain situations. So, in my opinion, yes, it is sufficient.
Back to top
View user's profile Send private message
FLiNG
Newbie cheater
Reputation: 0

Joined: 09 Apr 2011
Posts: 19

PostPosted: Thu Apr 14, 2011 4:10 pm    Post subject: Reply with quote

I am using C# and I alway use if, else if statements, works fine so far.
But I just started programming a week ago, I don't know which is better, I feel easier to use if statement.
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8585
Location: 127.0.0.1

PostPosted: Thu Apr 14, 2011 4:55 pm    Post subject: Reply with quote

There have been quiet a few debates about this over the past years with people doing performance checks and other comparisons to see which is better in the end.

Some reads:
http://www.parashift.com/c++-faq-lite/exceptions.html
http://www.joelonsoftware.com/items/2003/10/13.html
(Google for more if you wish.)

More info on try/catch/finally/throw:
http://msdn.microsoft.com/en-us/library/6dekhbbc%28v=VS.100%29.aspx
http://msdn.microsoft.com/en-us/library/9xtt5hxz%28v=VS.100%29.aspx

Overall it depends on your application though, if something can be caught with a simple if/else, you should use it rather then going with exception handling since it is overkill and not really needed. Such as what you are showing, a simple if can handle it.

If there is dynamic data coming into play that can cause your statements to throw exceptions (access violations, casting issues, etc.) then using try/catch becomes for feasible. For code quality, using try/catch should only come into play in most sensitive areas. If your program has the ability to crash at a given point due to unknown incoming data, sometimes it is best to use a try/catch to be sure. But if you are doing simple compares, your program has close to no chance of crashing unless it blows up or has some other extreme anomaly happen.

Another decent use of try/catch is with finally to cleanup multiple objects. If you have a rather in-depth function; for example DLL injecting; that is going to initialize and use various objects that in the end you will need to cleanup, as well as cleanup on each fail of another call then these can become useful.

In DLL injection you can have upto:
- Find process
- Open handle (Handle needs to be closed now.)
- Allocate memory. (Memory should be cleaned up afterward.)
- Write memory. (Can fail and require object cleanup.)
- CreateRemoteThread call to load the dll. (Creates a thread handle, can also fail.)
- Get the thread exit code to determine if it loaded. (Can fail; requiring cleanup.)
- Calling any exports if required. (Can also fail requiring more cleanup.)

Something this in depth can make more readable code by using a try/catch/finally statement to cleanup all objects in the finally block. In this example as well, it is very dynamic and not guaranteed that every operation will succeed.

Pseudo example:
Code:

bool Inject( param, param, param, etc )
{
    HANDLE hProcessHandle   = NULL;
    HANDLE hThreadHandle    = NULL;
    LPVOID lpAllocMemory    = NULL;
   
    try
    {
        hProcessHandle = OpenProcess( .. )
        if( hProcessHandle == NULL )
            throw ;
           
        lpAllocMemory = VirtualAllocEx( .. )
        if( lpAllocMemory == NULL )
            throw ;
           
        if( !WriteProcessMemory( .. ) )
            throw ;
           
        hThreadHandle = CreateRemoteThread( .. )
        if( hThreadHandle == NULL )
            throw ;
           
        WaitForSingleObject( .. )
       
        DWORD dwExitCode = NULL;
        GetExitCodeThread( hThreadHandle, &dwExitCode )
        if( dwExitCode == 0 )
            throw ;
           
        // other calls etc. here as needed..
           
        return true;
    }
    catch( ... )
    {
        // Handle exception accordingly..
        return false;
    }
    finally
    {
        if( hThreadHandle != NULL )
            CloseHandle( hThreadHandle )
           
        if( lpAllocMemory != NULL )
            VirtualFreeEx( .. )
       
        if( hProcessHandle != NULL )
            CloseHandle( hProcessHandle )
    }

}

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Thu Apr 14, 2011 5:04 pm    Post subject: Reply with quote

generally don't use exceptions as a form of flow control, use them for exceptional behavior. rarely a point if you know the outcome.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites