exception-handling

How to rescue page not found 404 in rails?

对着背影说爱祢 提交于 2019-12-30 16:36:13
问题 How to rescue page not found if user add wrong url in rails. I hope to show 404 page present in public folder if the url is invalid. How to do that? I was browsing about it but could not find a solution. I tried many ways to fix the problem, but they don't seem to be working. I am stuck here, please help. 回答1: Solution for Rails 4 On routes.rb : get '*unmatched_route', to: 'application#not_found' On application_controller.rb : def not_found # Your exception handling code here end 回答2: i found

Break a loop in OCaml

末鹿安然 提交于 2019-12-30 11:28:09
问题 I often need to break a loop in OCaml, there are at least two ways: (* by exception *) try for i = 0 to 100 do ... if cond then raise BreakLoop done; ... with BreakLoop -> ... (* by while *) let cond = ref false in let i = ref 0 in while (not !cond) && (i<= 100) do ... i := !i + 1 done; if !cond then ... What I care most is the optimisation of running time, as long as the program can be easily read and understood. The way while makes loops complicated when there are several nested loops. I

More terse error handling in Go

拈花ヽ惹草 提交于 2019-12-30 11:05:55
问题 How do I handle a lot of errors in Go? I look at my code and find that it is full of error handlers: err = result.Scan(&bot.BID, &bot.LANGUAGE, &bot.SOURCE) if err != nil { log.Fatalf("result.Scan: %v", err) return } fileName, err := copySourceToTemporaryFile(bot) if err != nil { log.Fatalf("copySourceToTemporaryFile: %v", err) return } ... And a lot of lines look like: // do something // handle error // handle error // handle error // do something 2 // handle error // handle error // handle

Return statements in catch blocks

放肆的年华 提交于 2019-12-30 09:31:12
问题 I have seen some developers use the return statement in a catch block. Why/when would this be a useful technique to employ? EDIT: I actually just saw the return keyword being used. Thanks 回答1: public void Function() { try { //some code here } catch { return; } } when return; is hit, the execution flow jumps out of the function. This can only be done on void methods. EDIT: you do this if you dont want to execute the rest of the function. For example if you are doing file IO and a read error

Exception handling for events

好久不见. 提交于 2019-12-30 08:30:39
问题 I apologize if this is a simple question (my Google-Fu may be bad today). Imagine this WinForms application, that has this type of design: Main application -> shows one dialog -> that 1st dialog can show another dialog. Both of the dialogs have OK/Cancel buttons (data entry). I'm trying to figure out some type of global exception handling, along the lines of Application.ThreadException. What I mean is: Each of the dialogs will have a few event handlers. The 2nd dialog may have: private void

Why use try and catch() in C++?

吃可爱长大的小学妹 提交于 2019-12-30 08:14:07
问题 I understand that try and catch() are used for exception handling, just in case an error or crash would occur in the program under certain cases. I also understand how they work. But why use try and catch() ? Why not just use an if() statement that looks for a certain case and if that case is true, it does cout << //error code ? 回答1: Exception handling: can be used with constructors and operators that have no opportunity to return a separate error code (they could set the object into some

Unhandled exception

耗尽温柔 提交于 2019-12-30 08:13:15
问题 What is the best way to handle an unhandled exception in a WPF application? 回答1: You can use DispatcherUnhandledException : XAML (App.xaml): <Application x:Class="App.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="wndMain.xaml" DispatcherUnhandledException="Application_DispatcherUnhandledException"> Code Behind (App.xaml.cs/vb: private void Application_DispatcherUnhandledException(object sender, System

When is a C++ terminate handler the Right Thing(TM)?

淺唱寂寞╮ 提交于 2019-12-30 08:08:09
问题 The C++ standard provides the std::set_terminate function which lets you specify what function std::terminate should actually call. std::terminate should only get called in dire circumstances, and sure enough the situations the standard describes for when it's called are dire (e.g. an uncaught exception). When std::terminate does get called the situation seems analagous to being out of memory -- there's not really much you can sensibly do. I've read that it can be used to make sure resources

How can I handle an access violation in Visual Studio C++?

蓝咒 提交于 2019-12-30 07:27:28
问题 Usually an access violation terminates the program and I cannot catch a Win32 exception using try and catch . Is there a way I can keep my program running, even in case of an access violation? Preferably I would like to handle the exception and show to the user an access violation occurred. EDIT: I want my program to be really robust, even against programming errors. The thing I really want to avoid is a program termination even at the cost of some corrupted state. 回答1: In Windows, this is

Does Spring's JdbcTemplate close the connection if an exception is thrown?

时光总嘲笑我的痴心妄想 提交于 2019-12-30 06:08:42
问题 When Spring catches an SQLException, does it close the prepared statement, result set, and/or connection before throwing it's own DataAccessException (runtime) exception? I have a developer who wants to create an AOP aspect to catch these exceptions and log and/or close the connection. @AfterThrowing(pointcut="dataAccessOperation()", throwing="exception") public void doRecoveryActions(JoinPoint thisJoinPoint, DataAccessException exception) { // log and/or close connection } 回答1: Yes. That's