try-catch

Throw exception in Java

放肆的年华 提交于 2019-12-24 00:52:12
问题 Suppose I have a class, the requirement is that "change the following code to throw checked exception if "den" is 0, and change the function to catch that exception". public class divide { void divide(int num, int den){ System.out.println(""+(num/den)); } void fun(){ divide(4,2); } } Which of the following one is the correct way to throw exception? Option 1: void divide(int num, int den) throws Exception{ if(den==0){ throw new Exception("Dividebyzero"); } System.out.println(""+(num/den)); }

What's the best way to deal with an error in the server side and in the client side using nodejs + express

巧了我就是萌 提交于 2019-12-24 00:40:03
问题 I'd like to know the best way to deal with errors in a response - request. I have this route that receive a request: app.get('/getInfo', function (req, res, next) { let obj = {} try { obj = { ... date: lastUpdatedDate('./utils/appVersion.js'), ... } res.status(200).send(obj) } catch (error) { console.log(error.message) res.send({error: "The data wasn't load"}) } }) And this function where the request is made getInfo () { axios.get(process.env.REACT_APP_HOST + '/getInfo') .then(resp => { this

Java BufferedReader error with try block

不问归期 提交于 2019-12-24 00:14:58
问题 I have this method that is supposed to load a file but it is giving me this error: NamnMetod.java:175: error: unreported exception FileNotFoundException; must be caught or declared to be thrown BufferedReader inFil = new BufferedReader(new FileReader(fil)); ^ NamnMetod.java:177: error: unreported exception IOException; must be caught or declared to be thrown String rad = inFil.readLine(); ^ This is my code: public static void hämtaFrånText() { try { EventQueue.invokeAndWait(new Runnable() {

R function return value as well warning message

时光怂恿深爱的人放手 提交于 2019-12-23 22:40:55
问题 I called other lib function, which does the calculation and throws warning message. I tried to use tryCatch() to capture the message but do not know how to keep the calculated value and the warning message. Here is sample (simplified) code, I would like mydiv function has both calculated value and warning message. Right now mydiv calls will return the division value or the warning but not both. mydiv = function(x, y){ tryCatch({ # raise warning message if (x > y) warning("throw a warning") #

How to handle exception without using try catch?

你说的曾经没有我的故事 提交于 2019-12-23 20:30:27
问题 using (SqlConnection con = new SqlConnection()) { try { con.Open(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } This works fine. But I want to know can we handle exception without using try catch like some thing if else ? or is it mendetory to use try catch 回答1: There is no other mechanism to handle an exception other than try catch. It sounds like you want something like if(connection.DidAnErrorOccur) but that doesn't exist. 回答2: Only way is to check for all the conditions that

MySQL query error “Illegal hour value” causing loop and write issues in Google Apps Script

我与影子孤独终老i 提交于 2019-12-23 20:12:56
问题 I'm not well-versed in either language, so please bear with me on this. I'm trying to pull a full table with 100 rows from a remote MySQL database into a Google Sheet. I've managed to sort all the issues I've been having with this, but finally feel stuck. It seems the "illegal hour value" error I get from the SQL query during the loop is the main problem. One of the columns in the MySQL database is "duration", and unfortunately, it can contain durations longer than 23:59:59. I only have

Stop Inserting in Table if record already exists

Deadly 提交于 2019-12-23 20:01:28
问题 I have sql server procedure, please see below. ALTER PROCEDURE [dbo].[uspInsertDelegate] ( @CourseID int, @CPUserID int, @StatusID int, @CreateUser varchar(25) ) AS SET NOCOUNT OFF; INSERT INTO tblDelegate ( CourseID, CPUserID, StatusID, CreateUser ) VALUES ( @CourseID, @CPUserID, @StatusID, @CreateUser ) RETURN Now I don't want to insert into table tblDelegate if the inserting courseid and cpuserid is same for that records in table tblDelegate 回答1: Simply test first. In SQL Server 2005 you

Suppress warnings using tryCatch in R

十年热恋 提交于 2019-12-23 14:41:19
问题 What I'm trying to do Write a tryCatch that will handle an error value but will ignore a warning. As an example foo <- function(x) { if (x == 1) { warning('Warning') } else if (x == 0) { stop('Error') } return(1) } bar <- function(x){ tryCatch( expr = foo(x), error = identity, warning = function(w) invokeRestart("muffleWarning") ) } So foo warns you if you pass a 0, and errors if you pass a 1. The intent of bar is that you get an error if you pass a 0, but it suppresses the warning generated

Python nested try/except - raise first exception?

帅比萌擦擦* 提交于 2019-12-23 12:06:42
问题 I'm trying to do a nested try/catch block in Python to print some extra debugging information: try: assert( False ) except: print "some debugging information" try: another_function() except: print "that didn't work either" else: print "ooh, that worked!" raise I'd like to always re-raise the first error, but this code appears to raise the second error (the one caught with "that didn't work either"). Is there a way to re-raise the first exception? 回答1: raise , with no arguments, raises the

How to prevent/catch connection error with MySQLi

◇◆丶佛笑我妖孽 提交于 2019-12-23 11:55:00
问题 How do you prevent a connection error if, for example, the database name is invalid, for the given code below? $mysql = new mysqli('localhost', 'user', 'pass', 'wrongdatabase'); if($mysql->connect_errno) die($mysql->connect_error); The if statement will output the right error message, but there will still be a warning sent from the first line, which says Warning: mysqli::mysqli() [<a href='mysqli.mysqli'>mysqli.mysqli</a>]: (42000/1049): Unknown database 'wrongdatabase' in C:\wamp\www\example