exception-handling

How to format Exception's stack trace in C#?

早过忘川 提交于 2019-12-05 18:51:51
问题 When Exception is displayed (or more general - when it is converted to String) stack trace is shown. What I want to do is to change the format of source code's file paths shown in such trace. Specifically I would like to leave filenames only (without full path) or show only that part of path which starts with project's base directory. Full paths are usually unnecessary and clutter the whole message. How can I do that? 回答1: I asked a similar question, although not entirely the same, here:

C++ - finding the type of a caught default exception

蓝咒 提交于 2019-12-05 18:44:57
问题 Say I have: try { externalLibrary::doSomething(); } catch (std::exception &e) { //yay I know what to do } catch (...) { //darn, I've no idea what happened! } There might be cases you get an exception and you don't know where it's coming from or why - in some external library with no debug info. Is there a way to find what was thrown, or otherwise obtain any data associated with it? They might be doing: throw myStupidCustomString("here is some really useful information"); But I'd never know if

Choosing between exception and return value

本小妞迷上赌 提交于 2019-12-05 18:43:42
问题 I have created a class that parses some document from file. class Parser { public Parse(string fileName) { /// } } Sometimes there can be parsing errors in that case parser has to return certain data. I have created special class for that. class ParsingError { // some data } How can I correctly deal with such errors. I have at least two options: create my own exception or return value. Option One myParser.Parse(fileName, out error); Option Two try { myParser.Parse(fileName) } catch

Weird issue with catching trivial boost exception

落花浮王杯 提交于 2019-12-05 18:35:35
I have an issue with the following straightforward code: try { boost::lexical_cast<unsigned int>("invalid string"); } catch(::boost::bad_lexical_cast &) { //It's not catched!!! return; } catch (std::exception &e){ std::cerr << boost::diagnostic_information(e) << std::endl; ::boost::bad_lexical_cast s; std::string ss = typeid(s).name(); std::cout << "'" << s.what()<<"': '"<< ss <<"'"; std::string ee = typeid(e).name(); std::cout << "'" << e.what()<<"': '"<< ee <<"'"; } The boost::bad_lexical_cast exception thrown by lexical_cast is somehow different than the one I try to catch, so the first

How to rescue from a OAuth::Unauthorized exception in a Ruby on Rails application?

感情迁移 提交于 2019-12-05 18:30:19
How can I rescue from an OAuth::Unauthorized exception as raised from OmniAuth in a Ruby on Rails application? Obviously this: rescue_from OAuth::Unauthorized, :with => :unauthorized won't work as that only catches exception thrown inside Rails and this exception is thrown somewhere else in the rack chain. In this application the administrators (and not us, the developers) configure the credentials for twitter and facebook, so having the wrong ones is something that can happen and indeed does happen. I'd like to show a better message that "Something went wrong" when that happens. Update : I

What purpose does the complexity of `Except` serve in Haskell?

痞子三分冷 提交于 2019-12-05 18:00:02
I understand (I think) that there is a close relationship between Either and Except in Haskell, and that it is easy to convert from one to the other. But I'm a bit confused about best practices for handling errors in Haskell and under what circumstances and scenarios I would choose one over the other. For example, in the example provided in Control.Monad.Except , Either is used in the definition type LengthMonad = Either LengthError so that calculateLength "abc" is Right 3 If instead one were to define type LengthMonad = Except LengthError then calculateLength "abc" would be ExceptT (Identity

More than one table found in namespace (, ) - SchemaExtractionException

主宰稳场 提交于 2019-12-05 17:42:28
问题 I have been facing this weird exception while trying to persist some values into a table using Hibernate in a Java application. However this exception occurs only for one particular table/entity for rest of the tables i am able to perform crud operations via Hibernate. Please find below the Stacktrace and let me know if this is anyway related to java code is or its a database design error. 2016-04-28 11:52:34 ERROR XXXXXDao:44 - Failed to create sessionFactory object.org.hibernate.tool.schema

Getting the Exception object in a try..catch to include FULL stacktrace, currently its truncated

99封情书 提交于 2019-12-05 17:38:05
can anyone help? I am trying to get the full stacktrace when within a catch of try..catch. Currently its truncated to include only the current method where the error is.... Let me explain.. Currently i my stacktrace includes the method "Third" where the error happens but First and Second isn't included, i believe this is by design. private void First() { this.Second(); } private void Second() { this.Third(); } private void Third() { try { throw new SystemException("ERROR HERE!"); } catch (Exception ex) { // I WILL LOG THE EXCEPTION object "EX" here ! but ex.StackTrace is truncated! } } I have

try: except: not working

妖精的绣舞 提交于 2019-12-05 17:13:36
问题 So I'm running into a problem where the try: except: mechanism doesn't seem to be working correctly in python. Here are the contents of my two files. pytest1.py import pytest2 class MyError( Exception ): def __init__( self, value ): self.value = value def __str__( self ): return repr( self.value ) def func1(): raise MyError( 'This is an error' ) def func3(): pytest2.func2() if __name__ == '__main__': try: func3() except MyError, e: print 'I should catch here.' except: print 'Why caught here?'

How can i get the error number instead of error message in an Exception in C#? [duplicate]

时光总嘲笑我的痴心妄想 提交于 2019-12-05 17:02:12
This question already has an answer here: How to get Exception Error Code in C# 5 answers Is there a way that i can get the corresponding error code of an Exceptions ? I need the thrown exceptions error code instead of its message , so that i based on the error code i show the right message to the user. If you're looking for the win32 error code, that's available on the Win32Exception class catch (Win32Exception e) { Console.WriteLine("ErrorCode: {0}", e.ErrorCode); } For plain old CLR exception, there is no integer error code. Given the problem you describe, I'd go with millimoose 's solution