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

  • SEARCH
  • Home
  • 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 way to improve this: private static int CountNewlines(string s) { int
Is there any way to make this work in Java? public static void change(List<?
Is there any way I can add a static extension method to a class.
Is there a way to combine static tableview cells (static content) with dynamic tableview
Is there a way to save a static password in a way that it's
Is there a way to force classes in Java to have public static final
Is there a simple way of making the static content of an .aspx page
Is there a way that I can add non-static data to dataannotation attributes (either
Is there a standard or common way in C++ to handle static strings that
Possible Duplicate: Returning in a static initializer Is there a way to exit a

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.