Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3852124
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:16:09+00:00 2026-05-19T17:16:09+00:00

Is there a static way of throwing an exception in php? I need to

  • 0

Is there a “static” way of throwing an exception in php?

I need to throw an exception when a mysql query fails.

I tried this:

$re=@mysql_query( $query ) or throw new Exception(' Query Failed ');

but it’s not working.

And I’m using a function based on the throwException() function from this comment at PHP: Exceptions manual, but I would like to know if there is a static way for doing this without making a class.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T17:16:09+00:00Added an answer on May 19, 2026 at 5:16 pm

    You won’t be able to directly do or throw new Exception(); because throw is a statement, not an expression. Since or is really an operator, it expects its operands to be expressions (things that evaluate to some values).

    You’d have to do this instead:

    $re = mysql_query($query);
    
    if (!$re) {
        throw new Exception('Query Failed');
    }
    

    If you’re trying to use the throwException() function proposed by that PHP manual comment, as webbiedave points out the comment is saying that you need to call that function instead of the throw statement directly, like this:

    $re = mysql_query($query) or throwException('Query Failed');
    

    There’s no rule in PHP that says you need to throw exceptions from a class method. As long as there’s some way to catch that exception you’re fine. If you mean you want to throw exceptions without using the Exception class, well, you have to. Exceptions are objects by nature; you can’t throw an exception that isn’t an object (or doesn’t inherit from the Exception class).

    If you don’t want to throw exceptions but raise the kind of error you often see from PHP (notices, warnings and fatal errors), use trigger_error().

    $re = mysql_query($query);
    
    if (!$re) {
        trigger_error('Query Failed', E_USER_ERROR);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a decent way with static HTML/XHTML to create common header/footer files to
What is the right way to perform some static finallization? There is no static
Is there a way to have a mutable static variable in F# class that
Is there a way to call Static Classes / Methods by name? Example: $name
Can I control the order static objects are being destructed? Is there any way
Is there a static analysis tool for PHP source files? The binary itself can
In C# there is the static property Environment.Newline that changed depending on the running
What tools are there available for static analysis against C# code? I know about
Is there anyway to have a sort of virtual static member in C++? For
Are there any tools that will spider an asp.net website and create a static

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.