try-catch

How to use try catch nicely in multiple methods?

北战南征 提交于 2019-12-10 16:58:47
问题 Sorry if my question is stupid, but I have this kind of code : public Object1 Method1(Object2 parameter) { try { return this.linkToMyServer.Method1(parameter); } catch (Exception e) { this.Logger(e); } return null; } public Object3 Method2(Object4 parameter) { try { return this.linkToMyServer.Method2(parameter); } catch (Exception e) { this.Logger(e); } return null; } /* ... */ public ObjectXX Method50(ObjectXY parameter) { try { return this.linkToMyServer.Method50(parameter); } catch

PHP 7 try - catch: unable to catch “Catchable fatal error”

╄→гoц情女王★ 提交于 2019-12-10 16:19:43
问题 I am playing with try - catch block: <?php try { $str = "http://rejstrik-firem.kurzy.cz/73631604"; $domOb = new DOMDocument(); $html = $domOb->loadHTMLFile($str); $domOb->preserveWhiteSpace = false; $container = $domOb->getElementById('ormaininfotab'); echo $container; // <========= this is intended error which I want catch } catch (Exception $e) { echo "Exception" . $e->getMessage() . ". File: " . $e->getFile() . ", line: " . $e->getLine(); } catch (Error $e) { echo "Error" . $e->getMessage(

When should we throw exceptions, or catch exceptions, in a method?

我只是一个虾纸丫 提交于 2019-12-10 15:59:52
问题 I've been reading up more on exceptions, but I am not sure with what cases we should either throw a method public void fxml() throws IOException { // method body here } or catch an exception within a method public void fxml() { FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("fxml.fxml")); try { fxmlLoader.load(); } catch (IOException exception) { throw new RuntimeException(exception); } } From Oracle's example it says Sometimes, it's appropriate for code to catch exceptions

java try-catch-finally recursion question

ぃ、小莉子 提交于 2019-12-10 15:50:54
问题 public class Foo { public static void main(String[] args) { foo(); } public static void foo() { try { System.out.println("try"); foo(); } catch (Throwable e) { System.out.println("catch"); foo(); } finally { System.out.println("finally"); foo(); } } } who can explain the output of this code ? 1.output on eclipse (endless) client mode: try try .... ... ... tryfinallyfinally tryfinallyfinally try try try tryfinallyfinally tryfinallyfinally try tryfinallyfinally tryfinallyfinally try .... .... 2

Use of unassigned local variable. But always falls into assignment

怎甘沉沦 提交于 2019-12-10 15:27:20
问题 having this code, I don't understand why if assigning a variable in a finally block doesn't understand it will ALWAYS be assigned. I think I missing a valid option where currency won't be assigned. If you know, will be great to understand why. much appreciate it! Thanks! CurrencyVO currency; try { if (idConnection.HasValue && idConnection != 0) { currencyConnection = client.GetConnection(idConnection.Value); model.Connection = currencyConnection; } else { int providerUserKey = (int)Models

How to handle errors in predict function of R?

无人久伴 提交于 2019-12-10 14:58:13
问题 I have a dataframe df, I am building an machine learning model (C5.0 decision tree) to predict the class of a column (loan_approved): Structure (not real data): id occupation income loan_approved 1 business 4214214 yes 2 business 32134 yes 3 business 43255 no 4 sailor 5642 yes 5 teacher 53335 no 6 teacher 6342 no Process: I randomly split the data frame into test and train, learned on train dataset (rows 1,2,3,5,6 train and row 4 as test) In order to account for new categorical levels in one

Swift try inside Objective-C block

不问归期 提交于 2019-12-10 14:57:06
问题 I need to create a function foo that takes a throwing closure as a parameter. I can implement it with either Swift or ObjC but I need to be able to call it from both. Like this: // Swift func bar() throws func foo(_ block: () throws -> void) foo { try bar() } and // Objc [self foo:^( [other barBar]; )]; I tried to implement it with both Swift and ObjC without succes. With Swift: @objc func foo(block: () throws -> Void) I get this error: Method cannot be marked @objc because the type of the

Closing streams/sockets and try-catch-finally

不问归期 提交于 2019-12-10 14:39:14
问题 Here is an example I have questions about (it comes from another SO question): public static void writeToFile (final String filename) { PrintWriter out = null; FileOutputStream fos = null; try { fos = new FileOutputStream(filename); out = new PrintWriter(new BufferedWriter( new OutputStreamWriter(fos, "UTF-8"))); for (final String line : log) { out.println(line); } out.flush(); out.close(); } catch (final Exception e) { System.err.println("Unable to write log to file."); } finally { if (fos !

Can't catch exception caused by C dll called via PInvoke

有些话、适合烂在心里 提交于 2019-12-10 13:28:01
问题 I'm writing a C# .NET 3.5 program wich uses the latest MediaInfoLib Dll. It seems that it causes an exception for some files. I want to catch those exceptions and ensure my program continues running, but for some reason I can't catch it with a simple try/catch statement. PInvoke Methods: [DllImport("MediaInfo.dll")] private static extern IntPtr MediaInfo_New(); [DllImport("MediaInfo.dll")] private static extern IntPtr MediaInfo_Open(IntPtr Handle,MarshalAs(UnmanagedType.LPWStr)] string

initialising a variable inside try block, a workaround?

一曲冷凌霜 提交于 2019-12-10 12:29:53
问题 I have a try-catch block; inside try , I read variable N from console and initialize an Array[N] . I need to use Array later. If I use it outside try block, I get an error java variable may not have been initialized . I understand it, but what should I do, write the whole program inside try block, really? Readability of such program is worse and I use try on the code where is no exceptions are possible. Is there a workaround? I tried a boolean variable which checks was there an exception and