try
		{
			switch($this->img["format"])
			{
				case "JPEG":

				//JPEG
				$this->img["src"] = @imagecreatefromjpeg ($imgfile);
			break;

			case "PNG":

				//PNG
				$this->img["src"] = @imagecreatefrompng ($imgfile);
			break;

			case "GIF":

				//GIF
				$this->img["src"] = @imagecreatefromgif ($imgfile);
			break;
		} // End Switch

	}
	catch
	{
		// Failsafe!
		if (@imagecreatefromjpeg($imgfile))
		{
			//JPEG
			$this->img["format"]="JPEG";
			$this->img["src"] = @imagecreatefromjpeg($imgfile);
		}
		elseif (@imagecreatefrompng($imgfile))
		{
			//JPEG
			$this->img["format"]="PNG";
			$this->img["src"] = @imagecreatefrompng($imgfile);
		}
		elseif (@imagecreatefromgif($imgfile))
		{
			//JPEG
			$this->img["format"]="GIF";
			$this->img["src"] = @imagecreatefromgif($imgfile);
		}
		elseif (@imagecreatefromwbmp($imgfile))
		{
			//JPEG
			$this->img["format"]="JPEG";
			$this->img["src"] = @imagecreatefromwbmp($imgfile);
		}
	}

OK, so you quite obviously can't use try... catch... like this!

What would I use to atempt to execute a block of code and on failure of ANY of them, execute the catch STYLE block?

Help?!**!??

    You'll want to use the defaul. From the manual: [man]switch[/man]

    switch ($i) {
    case 0:
        echo "i equals 0";
        break;
    case 1:
        echo "i equals 1";
        break;
    case 2:
        echo "i equals 2";
        break;
    default:
        echo "i is not equal to 0, 1 or 2";
    }
    

      I suppose you could use try/catch if you wanted, but you'd have to throw an exception for the catch block to "catch"...probably as part of a "default:" case. But that would seem unnecessarily complex since you could just put the code into that default block in the first place.

      try
      {
         switch($something)
         case 1:
            // do something
            break;
         case 2:
            // do something else
            break;
         default:
            throw new Exception('Oops!');
         }
      }
      catch(Exception $e)
      {
         /*
            do alternative code, which could just as easily be in the default
            block in the first place
         */
      }
      

        Hi and thanks.

        try 
        { 
           switch($something) 
           case 1: 
              // If $something==1 then it will execute here, great!
        	  // BUT: If it errors... I want it to jump to the catch, not if the $something != 1
        
          // Get it?
          break; 
           case 2: 
              // do something else 
              break; 
        
           default: 
              throw new Exception('Oops!'); 
           } 
        } 
        catch(Exception $e) 
        { 
           /* 
              do alternative code, which could just as easily be in the default 
              block in the first place 
           */ 
        }
        

        Check out the comment in Case 1

          You can do that, of course. Once control enters the case, it will not go to the other case due to the break. It will go to the exception handler block if an exception is thrown.

            Could you give me a short example?

              It could be something like:

              try
              {
                 switch($something)
                 case 1:
                    if ($error)
                    {
                       throw new Exception('Oops!');
                    }
                    break;
                 case 2:
                    // do something else
                    break; 
                 } 
              } 
              catch(Exception $e) 
              {
                 echo $e->getMessage();
              }

              Of course, instead of throwing an exception, the code might call a function from which an exception is thrown.

                In principle, if you're catching exceptions that you threw yourself, you're doing it wrong.

                  Weedpacket wrote:

                  In principle, if you're catching exceptions that you threw yourself, you're doing it wrong.

                  Could you try and explain what you mean, possibly, with an example of how you would correct it?

                    Weedpacket wrote:

                    In principle, if you're catching exceptions that you threw yourself, you're doing it wrong.

                    Any chance?

                      Could you try and explain what you mean, possibly, with an example of how you would correct it?

                      It does not make sense to throw and catch an exception from within the same locality as one could just check the error condition and handle it. For example, my earlier example would be better written as:

                      switch ($something)
                      {
                      case 1:
                          if ($error)
                          {
                              echo 'Oops!';
                          }
                          break;
                      case 2:
                          // do something else
                          break;
                      }

                      A better use of exceptions would be:

                      try
                      {
                          switch ($something)
                          {
                          case 1:
                              foo(); // could throw an exception
                              break;
                          case 2:
                              // do something else
                              break;
                          }
                      }
                      catch (Exception $e)
                      {
                          echo $e->getMessage();
                      }
                        iceomnia wrote:

                        Any chance?

                        Awfully sorry there, but I had some paying work that was occupying my time. I won't let it happen again. Maybe.

                          Write a Reply...