Author Topic: Having problems understanding the exceptions mechanism .  (Read 1805 times)

0 Members and 1 Guest are viewing this topic.

Offline pllaybuoy

  • Peasant
  • *
  • Posts: 70
  • Cookies: -3
    • View Profile
Having problems understanding the exceptions mechanism .
« on: April 11, 2013, 10:33:30 am »
Not that I haven't googled it already but I just don't seem the get the idea or when and where to use it , I am confused between the try and catch blocks too . Since it is said that anything inside the try block will be applied the exception tackling mechanism in throw block , then what is the catch block for ?
Can anybody explain this in the simplest of words ? and maybe , give an actual code and psuedo code example ?
"Monsters are real , ghosts are real too . They live inside us and sometimes they win"

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: Having problems understanding the exceptions mechanism .
« Reply #1 on: April 11, 2013, 03:53:20 pm »
Code: (c++) [Select]
"Hello my friend i am a terrorist and i will enter a very secure location"
void terrorist()
{
    if(stupidshitasshole)
    {
        throw new 1337;
     }
}

"Hello i am secure"
void securelocation()
{
    try{
        terrorist();
    }
    catch(int nLeetBomb)
    {
        std::cout << "He, i just catched the bomb before it did anything stupid.
    }
}
~Factionwars

Offline pllaybuoy

  • Peasant
  • *
  • Posts: 70
  • Cookies: -3
    • View Profile
Re: Having problems understanding the exceptions mechanism .
« Reply #2 on: April 11, 2013, 07:01:27 pm »
Code: (c++) [Select]
"Hello my friend i am a terrorist and i will enter a very secure location"
void terrorist()
{
    if(stupidshitasshole)
    {
        throw new 1337;
     }
}

"Hello i am secure"
void securelocation()
{
    try{
        terrorist();
    }
    catch(int nLeetBomb)
    {
        std::cout << "He, i just catched the bomb before it did anything stupid.
    }
}

So the throw exception was by terrorist ? And the try block is used to check a  code for error , but I still don't get the catch part
"Monsters are real , ghosts are real too . They live inside us and sometimes they win"

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: Having problems understanding the exceptions mechanism .
« Reply #3 on: April 11, 2013, 07:05:28 pm »
So the throw exception was by terrorist ? And the try block is used to check a  code for error , but I still don't get the catch part
The try block tells you. Hey I am going to run this code where I might get a exception from. Then the catch block catches the error if the try block find a exception. And the catch block can handle the exception (error) by for example tell the user he gave unexpected input.
~Factionwars

Offline pllaybuoy

  • Peasant
  • *
  • Posts: 70
  • Cookies: -3
    • View Profile
Re: Having problems understanding the exceptions mechanism .
« Reply #4 on: April 11, 2013, 07:59:33 pm »
The try block tells you. Hey I am going to run this code where I might get a exception from. Then the catch block catches the error if the try block find a exception. And the catch block can handle the exception (error) by for example tell the user he gave unexpected input.

The try block can run some code where it gets an exception , right ? then what is the need for catch block ? When the try block can find and deal with the exception itself ?
"Monsters are real , ghosts are real too . They live inside us and sometimes they win"

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: Having problems understanding the exceptions mechanism .
« Reply #5 on: April 11, 2013, 08:02:34 pm »
The try block can run some code where it gets an exception , right ? then what is the need for catch block ? When the try block can find and deal with the exception itself ?

The try block can't handle a exception. The exception is CATCHED, by the CATCH block. See ? the try runs code and if that code throws a exception, it is catched by the catch block.
~Factionwars

Offline pllaybuoy

  • Peasant
  • *
  • Posts: 70
  • Cookies: -3
    • View Profile
Re: Having problems understanding the exceptions mechanism .
« Reply #6 on: April 11, 2013, 08:05:15 pm »
The try block can't handle a exception. The exception is CATCHED, by the CATCH block. See ? the try runs code and if that code throws a exception, it is catched by the catch block.
Alright , getting it . Can you give a simple source code example which I can compile and see for myself ?
"Monsters are real , ghosts are real too . They live inside us and sometimes they win"

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: Having problems understanding the exceptions mechanism .
« Reply #7 on: April 11, 2013, 08:12:06 pm »
Code: (c++) [Select]
#include <iostream>

int main()
{
    //Integer monkey is the user input,
    // I expect the user to input a int between 1-5
    signed int monkey = 6;

    try
    {
if(monkey < 1 || monkey > 5)
{
throw new 3;
}    

std::cout << "Thanks for your input it is correct";
    }
    catch(int except)
    {
    switch(except)
    {
    case 1:
    std::cout << "You are a moron";
    break;
    case 2:
    std::cout << "I don't like you";
    break;
    case 3:
    std::cout << "Please enter a number between 1-5";
    break;
    }
    }
    return 0;
}

This should do mate :)
~Factionwars

Offline pllaybuoy

  • Peasant
  • *
  • Posts: 70
  • Cookies: -3
    • View Profile
Re: Having problems understanding the exceptions mechanism .
« Reply #8 on: April 11, 2013, 08:16:20 pm »
Code: (c++) [Select]
#include <iostream>

int main()
{
    //Integer monkey is the user input,
    // I expect the user to input a int between 1-5
    signed int monkey = 6;

    try
    {
      if(monkey < 1 || monkey > 5)
      {
         throw new 3;
      }     

      std::cout << "Thanks for your input it is correct";
    }
    catch(int except)
    {
       switch(except)
       {
          case 1:
             std::cout << "You are a moron";
             break;
          case 2:
             std::cout << "I don't like you";
             break;
          case 3:
             std::cout << "Please enter a number between 1-5";
             break;
       }
    }
    return 0;
}

This should do mate :)

Got it dude,  thanks a lot . And is the new 3 keyword part of a throw statement ?
"Monsters are real , ghosts are real too . They live inside us and sometimes they win"

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: Having problems understanding the exceptions mechanism .
« Reply #9 on: April 11, 2013, 08:21:27 pm »
Got it dude,  thanks a lot . And is the new 3 keyword part of a throw statement ?
That means it throws "3" which is an integer in this case. Thus the corresponding catch (int) who only catches a integer. You could throw for example a std::string and a int on different cases and catch handle them in their own way using
Code: (c) [Select]
try
{
    if(1==1)
    {
         throw new "I will always get thrown.. poor int";
    }
    else
    {
        throw new 1337;
     }
}
catch(int exception)
{
}
catch(std::string exception)
{
}
~Factionwars

Offline pllaybuoy

  • Peasant
  • *
  • Posts: 70
  • Cookies: -3
    • View Profile
Re: Having problems understanding the exceptions mechanism .
« Reply #10 on: April 11, 2013, 08:24:38 pm »
Got it , thanks dude .This helped a lot . +1
"Monsters are real , ghosts are real too . They live inside us and sometimes they win"

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: Having problems understanding the exceptions mechanism .
« Reply #11 on: April 11, 2013, 08:31:49 pm »
Got it , thanks dude .This helped a lot . +1
Glad to help you out :)
~Factionwars