小编kir*_*sos的帖子

除块外引发异常并抑制第一个错误

我正在尝试捕获异常并在我的代码中的某个点引发更具体的错误:

try:
  something_crazy()
except SomeReallyVagueError:
  raise ABetterError('message')
Run Code Online (Sandbox Code Playgroud)

这适用于Python 2,但在Python 3中,它显示了两个例外:

Traceback(most recent call last):
...
SomeReallyVagueError: ...
...

During handling of the above exception, another exception occurred:

Traceback(most recent call last):
...
ABetterError: message
...
Run Code Online (Sandbox Code Playgroud)

有没有办法绕过这个,所以SomeReallyVagueError没有显示回溯?

python exception-handling python-3.x

9
推荐指数
1
解决办法
3224
查看次数

从[Maybe a]中提取第一个Just值

说我有一个列表,如:

[Nothing, Just 1, Nothing, Just 2]
Run Code Online (Sandbox Code Playgroud)

我想得到第一个Just(非错误)值; 在这种情况下,它是Just 1.我唯一能想到的是:

firstJust xs = case filter isJust xs of
                 []     -> Nothing
                 Just x -> Just x
Run Code Online (Sandbox Code Playgroud)

有没有更好的/ monad-generic方法来做到这一点?

monads haskell maybe

7
推荐指数
1
解决办法
1573
查看次数

如何在Mac OS上构建简单的升级程序(Lion)

步骤:
1.sudo port boost
安装在/ opt/local/boost中的boost文件,库文件在/ opt/local/lib中

2.使用XCode创建c ++项目

#include <iostream>
#include <boost/asio.hpp>
int main () {
    return 0;
}
Run Code Online (Sandbox Code Playgroud)


3.设置XCode以
在"构建设置" - >"HEADER_SEARCH_PATHS"
中找到提升,在Debug和Release中添加路径/ opt/local/include

4."Build Settings" - >"LIBRARY_SEARCH_PATHS" - > add/opt/local/lib都用于调试和发布.

5.构建程序并失败.
错误消息,

Undefined symbols for architecture x86_64:
  "boost::system::generic_category()", referenced from:
  ___cxx_global_var_init1 in main.o
  ___cxx_global_var_init2 in main.o
  "boost::system::system_category()", referenced from:
  ___cxx_global_var_init3 in main.o
  boost::asio::error::get_system_category() in main.o
  "boost::asio::error::get_netdb_category()", referenced from:
  ___cxx_global_var_init5 in main.o <br>
  "boost::asio::error::get_addrinfo_category()", referenced from:
  ___cxx_global_var_init6 in main.o <br>
  "boost::asio::error::get_misc_category()", referenced from:
  ___cxx_global_var_init7 in main.o <br>
ld: …
Run Code Online (Sandbox Code Playgroud)

boost osx-lion

6
推荐指数
1
解决办法
6329
查看次数

Clang 行指令

我正在编写一个输出 C++ 代码的语法翻译器,但遇到了一个有趣的问题。假设我有两个文件:ln.xln.cpp. 在ln.x

abc
Run Code Online (Sandbox Code Playgroud)

ln.cpp

#line 1 "ln.x"
(
Run Code Online (Sandbox Code Playgroud)

当我尝试使用 GCC 编译它时,它会在ln.x以下位置打印相应的行:

ln.x:1:1: error: expected unqualified-id at end of input
 abc
 ^
ln.x:1:1: error: expected ‘)’ at end of inpu
Run Code Online (Sandbox Code Playgroud)

但是,Clang 只是打印同一文件的行:

ln.x:1:2: error: expected unqualified-id
(
 ^
ln.x:1:2: error: expected ')'
ln.x:1:1: note: to match this '('
(
^
2 errors generated.
Run Code Online (Sandbox Code Playgroud)

有没有办法让 Clang 像 GCC 一样打印文件的行?

c++ gcc g++ clang clang++

5
推荐指数
1
解决办法
593
查看次数

命令中的“核心”是什么:rm -f *~ core $(INCDIR)/*~?

我正在学习Makefile!我包含了整个rm命令,以防某些参数相互依赖:

\n\n
rm -f \\*~ core $(INCDIR)/\\*~\n
Run Code Online (Sandbox Code Playgroud)\n\n

我假设 C++ 生成一些以 \'~\' 结尾的文件,所以我们删除这些文件,但是core是什么?谷歌返回的唯一内容是似乎假设其功能已经已知的教程,但我找不到任何只是说“核心是......”的内容。

\n\n

Rider:假设“_OBJ”是目标文件列表,“ODIR”是一个目录。然后...

\n\n
$(patsubst %, $(ODIR)/%, $(_OBJ))\n
Run Code Online (Sandbox Code Playgroud)\n\n

... 获取 \'_OBJ\' 中的任何文件名fname并将其替换为 \'$(ODIR)/fname\'\xe2\x80\x94,从而将其移动到目录 \'$(ODIR)\'名称fname,对吗?

\n

c++ makefile

5
推荐指数
1
解决办法
3013
查看次数

实例没有属性_pi

我正在尝试制作这样的Python模块:

class square:
    def _init_(self):
        self._length = 0
        self._perimeter = 0
        self._area = 0
    def setLength(self, length):
        self._length = float(length)
        self._perimeter = 0
        self._area = 0
    def getLength(self):
        return self._length
    def getPerimeter(self):
        if self._perimeter == 0:
            self._perimeter = float(self._length * 4)
        return self._perimeter
    def getArea(self):
        if self._area == 0:
            self._area = float(self._length * self._length)
        return self._area
class rectangle:
    def _init_(self):
        self._length = 0
        self._width = 0
        self._perimeter = 0
        self._area = 0
    def setLength(self, length):
        self._length = float(length)
        self._perimeter = …
Run Code Online (Sandbox Code Playgroud)

python class package attributeerror python-2.7

1
推荐指数
1
解决办法
616
查看次数

无法匹配预期的类型

我有以下Haskell程序:

catlines = unlines . zipWith (\(n,l) -> show n ++ l) [0..]

main = putStrLn $ catlines ["A", "B"]
Run Code Online (Sandbox Code Playgroud)

当我尝试编译它时,GHC给出以下错误:

catlines.hs:1:41:
    Couldn't match expected type `b0 -> String' with actual type `[a0]'
    In the expression: show n ++ l
    In the first argument of `zipWith', namely
      `(\ (n, l) -> show n ++ l)'
    In the second argument of `(.)', namely
      `zipWith (\ (n, l) -> show n ++ l) [0 .. ]'
Run Code Online (Sandbox Code Playgroud)

据我所知,这应该编译.我不知道出了什么问题.

haskell ghc

0
推荐指数
1
解决办法
143
查看次数

Perl非法划分为零

我正在尝试运行这个Perl程序.

#!/usr/bin/perl
# ---------------     exchange.pl     -----------------

&read_exchange_rate;     # read exchange rate into memory

# now let's cycle, asking the user for input...

print "Please enter the amount, appending the first letter of the name of\n";
print "the currency that you're using (franc, yen, deutschmark, pound) -\n";
print "the default value is US dollars.\n\n";
print "Amount: ";

while (<STDIN>) {

  ($amnt,$curr) = &breakdown(chop($_));

  $baseval = $amnt * (1/$rateof{$curr});

  printf("%2.2f USD, ", $baseval * $rateof{'U'});
  printf("%2.2f Franc, ", $baseval * $rateof{'F'});
  printf("%2.2f …
Run Code Online (Sandbox Code Playgroud)

unix perl activestate divide-by-zero

-1
推荐指数
1
解决办法
1828
查看次数