throw

PHP Fatal error: Uncaught exception 'Exception'

﹥>﹥吖頭↗ 提交于 2019-12-18 05:45:14
问题 I'm playing around with exceptions in PHP. For example, I have a script that reads a $_GET request and loads a file; If the file doesn't exists, an new exception should be thrown: if ( file_exists( $_SERVER['DOCUMENT_ROOT'] .'/'.$_GET['image'] ) ) { // Something real amazing happens here. } else { throw new Exception("The requested file does not exists."); } The problem is that, when I try to supply an non existent file for the test, I got a 500 error instead of the exception message. The

Forwarding an error in Swift

佐手、 提交于 2019-12-17 21:25:32
问题 Is there a better solution to forward a Swift error from one function to another? In the moment, I'm doing it like this: enum Error:ErrorType{ case Error1 case Error2 } func func1()throws{ do{ try func2() }catch Error.Error1{ throw Error.Error1 }catch Error.Error2{ throw Error.Error2 } } func func2()throws{ // proof something throw Error.Error1 } So, to forward an error, I need to catch all the errors and throw them again. Is there a better solution? 回答1: Yes: don't wrap it in a do ... catch

How do exceptions work (behind the scenes) in c++

对着背影说爱祢 提交于 2019-12-17 02:52:22
问题 I keep seeing people say that exceptions are slow, but I never see any proof. So, instead of asking if they are, I will ask how do exceptions work behind the scene, so I can make a decisions of when to use them and if they are slow. From what I know, exceptions are the same thing as doing a bunch of return but it also checks when it needs to stop doing the return. How does it check when to do stop? I am taking a guess and saying there is a second stack which holds the type of exception and

Exception handling : throw, throws and Throwable

我的未来我决定 提交于 2019-12-17 02:28:49
问题 Can any of you explain what the differences are between throw , throws and Throwable and when to use which? 回答1: throws : Used when writing methods, to declare that the method in question throws the specified (checked) exception. As opposed to checked exceptions, runtime exceptions (NullPointerExceptions etc) may be thrown without having the method declare throws NullPointerException . throw : Instruction to actually throw the exception. (Or more specifically, the Throwable ). The throw

catch exception by pointer in C++

我与影子孤独终老i 提交于 2019-12-17 02:11:49
问题 I found that there are three ways to catch an exception, what are the differences? 1) catch by value; 2) catch by reference; 3) catch by pointer; I only know that catch by value will invoke two copies of the object, catch by reference will invoke one. So how about catch by pointer? When to use catch by pointer? In addition to throw an object, can I throw a pointer to an object like this? class A {} void f() { A *p = new A(); throw p; } 回答1: The recommended way is to throw by value and catch

Static type of the exception object

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-14 01:38:37
问题 I read the following from C++ Primer (5th edition, Section 18.1.1): "When we throw an expression, the static, compile-time type of that expression determines the type of the exception object." So I tried the following code: #include <iostream> class Base{ public: virtual void print(std::ostream& os){os << "Base\n";} }; class Derived: public Base{ public: void print(std::ostream& os){os << "Derived\n";} }; int main(){ try{ Derived d; Base &b = d; b.print(std::cout); //line 1 throw b; } catch

How do I Throw an IllegalArgumentException that won't terminate my program?

烂漫一生 提交于 2019-12-12 18:18:09
问题 Ok so I have a method with a switch statement but I left out the rest of the cases because they're not important. In my main method, the operator method is called and passed the parameter "selection" in a while loop until they choose "Q". When the user enters a negative number, it should throw an exception, print a message, and ignore their input but then loop back to the beginning. When this exception is thrown it terminates the program. Any help would be very much appreciated. Thanks!

gdb: step over throw statement in C++

北战南征 提交于 2019-12-12 17:22:50
问题 When debugging a C++ program with the GNU gdb debugger, I can step over the next line of code with the gdb command next However, when an exception is thrown within that next line, like e.g. throw SomeException(); then gdb continues to run until the next breakpoint instead of stopping within the first line of the catch block. Is this a bug within gdb, or am I just using the wrong command? I'm using gdb version 7.7 on mingw32 / Windows. 回答1: On Linux this works properly. In particular there is

Does Safari support javascript window.onerror?

痞子三分冷 提交于 2019-12-12 12:29:50
问题 I have a function attached to window.onerror window.onerror = function(errorMsg, url, line) { window.alert('asdf'); };"; This works fine in firefox, chrome and IE, but it doesn't work in safari. From some digging I read somewhere that safari does not support onerror. The post however was a few years old. Does safari currently support onerror? If not, is there a workaround? 回答1: Yes, Safari does support window.onerror with the function signature you posted: function(errorMsg, url, line) ,

When should I use a ThrowHelper method instead of throwing directly?

╄→гoц情女王★ 提交于 2019-12-12 08:20:31
问题 When is it appropriate to use a ThrowHelper method instead of throwing directly? void MyMethod() { ... //throw new ArgumentNullException("paramName"); ThrowArgumentNullException("paramName"); ... } void ThrowArgumentNullException(string paramName) { throw new ArgumentNullException(paramName); } I've read that calling a ThrowHelper method (a method with the only purpouse of throwing an exception) instead of throwing directly should yield smaller bytecode. This, and the obvious encapsulation