exception-handling

php custom exceptions

微笑、不失礼 提交于 2019-12-06 00:42:27
问题 I was wondering how would one go about writing custom exception handlers. so that I can do something like throw new dbException($sql, $message); and have it output There was an error in your query Message: {$message here} Query: {$sql here} Line: {line exception was thrown on} File: {file exception was thrown from} but I also want to to catch eg syntax errors and parse errors (if possible) 回答1: Well, you can extend the Exception class however you like. For custom exceptions, you might want to

(Windows) Exception Handling: to Event Log or to Database?

不问归期 提交于 2019-12-06 00:36:50
问题 I've worked in shops where I've implemented Exception Handling into the event log, and into a table in the database. Each have their merits, of which I can highlight a few based on my experience: Event Log Industry standard location for exceptions (+) Ease of logging (+) Can log database connection problems here (+) Can build report and viewing apps on top of the event log (+) Needs to be flushed every so often, if alot is reported there (-) Not as extensible as SQL logging [add custom fields

Handle URI hacking gracefully in ASP.NET

三世轮回 提交于 2019-12-06 00:25:10
I've written an application that handles most exceptions gracefully, with the page's design intact and a pretty error message. My application catches them all in the Page_Error event and there adds the exception to HttpContext.Curent.Context.Items and then does a Server.Transfer to an Error.aspx page. I find this to be the only viable solution in ASP.NET as there seems to be no other way to do it in a centralized and generic manner. I also handle the Application_Error and there I do some inspection on the exception that occurred to find out if I can handle it gracefully or not. Exceptions I've

How to properly leave a Critical Section?

混江龙づ霸主 提交于 2019-12-05 23:43:51
问题 I have the following C++ code where I make use of the Critical Section object: EnterCriticalSection(&cs); // code that may throw an exception LeaveCriticalSection(&cs); How can I ensure that the LeaveCriticalSection function is called even if an exception is thrown? 回答1: Just write a guard utilizing the destructor for clean up: struct Guard { CriticalSection& cs; Guard(CriticalSection& cs) : cs(cs) { EnterCriticalSection(cs); } ~Guard() { LeaveCriticalSection(cs); } Guard(const Guard&) =

Exception handling with get_meta_tags() & get_headers()?

吃可爱长大的小学妹 提交于 2019-12-05 22:41:59
In PHP, I am using get_meta_tags() and get_headers() , however, when there is a 404, those two functions throw a warning. Is there any way for me to catch it? Thanks! get_headers does not throw a Warning/Error on 404, but get_meta_tags does. So you can check the header response and do something, when it's not OK: $url = 'http://www.example.com/'; $headers = array(); $metatags = array(); $validhost = filter_var(gethostbyname(parse_url($url,PHP_URL_HOST)), FILTER_VALIDATE_IP); if($validhost){ // get headers only when Domain is valid $headers = get_headers($url, 1); if(substr($headers[0], 9, 3) =

Is it good practice to have Generic Exception class handler in global exception handler with spring rest?

醉酒当歌 提交于 2019-12-05 22:26:55
I was referring few articles to create global exception handler using @ControllerAdvice for my rest api project using spring. The purpose of this is to send proper formatted response to the client in the case of exception occurred. In some articles they have added Throwable or Exception in global exception handler. Should I replace it with RunTimeException as this block is for exception occurred at runtime? Exception Handler code: @ControllerAdvice public class GlobalExceptionHandler{ @ExceptionHandler(NoDataFoundException.class) @ResponseStatus(code=HttpStatus.NOT_FOUND) public ResponseEntity

How should Iterator implementation deal with checked exceptions?

百般思念 提交于 2019-12-05 22:04:34
问题 I'm wrapping a java.sql.RecordSet inside a java.util.Iterator. My question is, what should I do in case any recordset method throws an SQLException? The java.util.Iterator javadoc explains which exceptions to throw in various situations (i.e. NoSuchElementException in case you call next() beyond the last element) However, it doesn't mention what to do when there is an entirely unrelated problem caused by e.g. network or disk IO problems. Simply throwing SQLException in next() and hasNext() is

ofstream exception handling

假装没事ソ 提交于 2019-12-05 21:58:26
问题 Deliberately I'm having this method which writes into a file, so I tried to handle the exception of the possiblity that I'm writing into a closed file: void printMe(ofstream& file) { try { file << "\t"+m_Type+"\t"+m_Id";"+"\n"; } catch (std::exception &e) { cout << "exception !! " << endl ; } }; But apparently std::exception is not the appropriate exception for a closed file error because I deliberately tried to use this method on an already closed file but my "exception !! " comment was not

@ExceptionHandler for all controllers in spring-mvc

大憨熊 提交于 2019-12-05 21:45:40
I have wrote following controller to handle all exception in my code: @Controller public class ErrorHandlerController { @ExceptionHandler(value = Exception.class) public String redirectToErrorPage(Model model){ model.addAttribute("message", "error on server"); return "errorPage"; } } But looks like following exception handler will work only if exception throws inside ErrorHandlerController I have a huge count of controllers. Please advice me how to write one ExceptionHandler for all controllers ? P.S. I understand that I can use inheritance but I don't sure that it is the best decision. Change

Spring MVC ExceptionHandler for restful and normal

£可爱£侵袭症+ 提交于 2019-12-05 21:35:23
I want to handle exception for both normal and rest/ajax requests. Here is my code, @ControllerAdvice public class MyExceptionHandler { @ExceptionHandler(Exception.class) public ModelAndView handleCustomException(Exception ex) { ModelAndView model = new ModelAndView("error"); model.addObject("errMsg", ex.getMessage()); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); ex.printStackTrace(pw); sw.toString(); model.addObject("errTrace", sw); return model; } @ExceptionHandler(Exception.class) @ResponseBody public String handleAjaxException(Exception ex) { JSONObject model