try-catch

PHP Notice: Undefined index although using try\catch

南楼画角 提交于 2019-12-06 17:09:12
问题 This is my try/catch block in PHP: try { $api = new api($_GET["id"]); echo $api -> processRequest(); } catch (Exception $e) { $error = array("error" => $e->getMessage()); echo json_encode($error); } When there is nothing in the $_GET["id"] , I still get the notice error. How can I avoid getting this error? 回答1: use isset function to check if the variable is set or not: if( isset($_GET['id'])){ $api = new api($_GET["id"]); echo $api -> processRequest(); } 回答2: If you want a fast and "dirty"

Redoing a try after catch in Java

删除回忆录丶 提交于 2019-12-06 15:54:52
import java.util.Scanner; public class Questioner { Scanner scanner = new Scanner(System.in); boolean condition; int tempInt; double tempDouble; public Questioner() { condition = true; } public String stringInput(String text) { System.out.print(text); return scanner.nextLine(); } public int intInput(String text) { do { System.out.print(text); try { tempInt = scanner.nextInt(); condition = false; } catch (java.util.InputMismatchException error) { System.out.println("Please use valid input."); } } while (condition == true); return tempInt; } public double doubleInput(String text) { System.out

how to handle errors while reading xml files R

别来无恙 提交于 2019-12-06 12:04:52
I have a list of multiple xml files which have the same structure. Some of them have structural errors in them so they can't be read, i'm not capable of controlling them manually because there are too many files. I know that i need to imply the try or trycatch functions, i tried to understand them but i'm not understanding how to use them proberly on my case. To make the example easy i just want to transform them all into a csv. library(XML) k <- 1 Initial.files<- list.files("/My/Initial/Folder") for(i in initial.files){ data<-dataTable(xmlToDataFrame(xmlParse(i))) write.csv(data, file = paste

tryCatch with a complicated function and plyr in R

时光毁灭记忆、已成空白 提交于 2019-12-06 11:11:24
I've got a complicated, long function that I'm using to do simulations. It can generate errors, mostly having to do with random vectors ending up with equal values with zero-variance, getting fed either into PCA's or logistic regressions. I'm executing it on a cluster using doMC and plyr . I don't want to tryCatch every little thing inside of the function, because the possibilities for errors are many and the probabilities of each of them are small. How do I tryCatch each run, rather than tryCatch ing every little line?? The code is something like this: iteration = function(){ a really long

Divide PHP errors and Application Errors

懵懂的女人 提交于 2019-12-06 10:55:27
问题 I'm working on an Application and a question occurred. I was thinking of letting PHP errors a side (they would be log in the database or in a file) and manage other errors (such as "Your username is not valid" or "Your typed the wrong password" or "The image could not be loaded") with the so famous try-catch method. Is it good to completely manage this kind of errors only with try-catch? 回答1: The main advantage of exceptions is it's behavior of "petite mort", a local die() inside try{} block,

Database Error Handling problem in CodeIgniter

ぐ巨炮叔叔 提交于 2019-12-06 09:13:38
问题 I use CodeIgniter as my web application framework. I used a simple Try/Catch and I sent a sample value to test it, and it failed! I know I can use $this->db->escape() function to solve my data problem but I just want to know: Why TRY/CATCH can not catch this error! Controler code: $this->load->model('user_model'); $result = $this->user_model->test_user("tes'ti"); Model code: function test_user($username){ try { $query_str = "SELECT * FROM tbl_user WHERE username = '".$username."'"; $result =

Can I Try-Catch PHP Warnings while loading an html file into DOMDocument?

强颜欢笑 提交于 2019-12-06 08:39:39
问题 Is it possible to do some sort of try catch that will catch warnings? e.g. if (!$dom->loadHTMLFile($url)) { //if cant load file handle error my way } For the $url I am using I am getting Warning (2): DOMDocument::loadHTMLFile(MYURL) [domdocument.loadhtmlfile]: failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden [APP\controllers\import_controller.php, line 62] Warning (2): DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: I/O warning : failed to load external entity

Node.js / Mongoose - Undo Database Modifications on Error

百般思念 提交于 2019-12-06 08:26:37
I have quite a complex express route in my node.js server, that makes many modifications to the database via mongoose . My goal is to implement a mechanism, that reverts all changes when any error occurs. My idea was implementing functions for undoing into the catch block. But this is quite ugly, as I have to know what the previous values were and what if an error occurs in the catch block? It's especially difficult to revert those changes, when an error occurred during a Promise.all(array.map( /* ... */ )) My route looks akin to this: module.exports = (req, res) => { var arr1, var2, var3 try

sql error not thrown back to caller

五迷三道 提交于 2019-12-06 08:13:53
问题 I am new to this forum but please bear with me. I have a c# Windows form which has two checkboxes on it One is called chkThrowError and the other is called chkDivideError, both are unchecked. These controls are purely there to control execution of a stored procedure. I have a command Button with the following Code: private void cmdError_Click(object sender, EventArgs e) { int ThrowError = 0; int DividByZero = 0; if (chkThrowError.Checked) { ThrowError = 1; } if (chkDivideError.Checked) {

Catch QML error message

本小妞迷上赌 提交于 2019-12-06 07:34:21
I'm using Qt.createQmlObject() to create a QML object from a file. In the case the file is corrupted, QML outputs a message that looks like this: Qt.createQmlObject(): failed to create object: qrc:/graphics/inline:5:2: Expected token }'` I'd like to catch the message in order to tell the user that his file is corrupted. I am trying to use the third argument provided in the Qt.createQmlObject() but I don't understand how it works. The wiki describes the function quite well but doesn't give any example exploiting it: https://wiki.qt.io/QML_Dynamic_Objects#Creation_of_Dynamic_QML_Objects The