Questions tagged [except]

Used for the MS .NET LINQ Except() method and the Python except keyword for exception handling. Use the tag sql-except for questions related to the SQL operator EXCEPT

This tag is used for two different things:

  1. The Microsoft [.NET] Framework [LINQ] Except() method which produces set differences of two sequences.

  2. The [Python] keyword except used for exception handling.

Please use the tag for questions related to the SQL operator EXCEPT

598 questions
896
votes
15 answers

python exception message capturing

import ftplib import urllib2 import os import logging logger = logging.getLogger('ftpuploader') hdlr = logging.FileHandler('ftplog.log') formatter = logging.Formatter('%(asctime)s %(levelname)s…
Hellnar
  • 62,315
  • 79
  • 204
  • 279
283
votes
33 answers

Safe method to get value of nested dictionary

I have a nested dictionary. Is there only one way to get values out safely? try: example_dict['key1']['key2'] except KeyError: pass Or maybe python has a method like get() for nested dictionary ?
Arti
  • 7,356
  • 12
  • 57
  • 122
129
votes
10 answers

Multiple try codes in one block

I have a problem with my code in the try block. To make it easy this is my code: try: code a code b #if b fails, it should ignore, and go to c. code c #if c fails, go to d code d except: pass Is something like this possible?
arvidurs
  • 2,853
  • 4
  • 25
  • 36
120
votes
4 answers

Using python "with" statement with try-except block

Is this the right way to use the python "with" statement in combination with a try-except block?: try: with open("file", "r") as f: line = f.readline() except IOError: If it is, then considering the old way of doing…
new name
  • 15,861
  • 19
  • 68
  • 114
107
votes
2 answers

What is wrong with using a bare 'except'?

I tried making a function to check if an image is displayed on the screen using PyAutoGui and came up with this: def check_image_on_screen(image): try: pyautogui.locateCenterOnScreen(image) return True except: return…
CaioRamaglio
  • 1,197
  • 2
  • 6
  • 6
57
votes
7 answers

Catch KeyError in Python

If I run the code: connection = manager.connect("I2Cx") The program crashes and reports a KeyError because I2Cx doesn't exist (it should be I2C). But if I do: try: connection = manager.connect("I2Cx") except Exception, e: print e It…
spizzak
  • 1,087
  • 1
  • 11
  • 17
48
votes
6 answers

Python try block does not catch os.system exceptions

I have this python code: import os try: os.system('wrongcommand') except: print("command does not work") The code prints: wrongcommand: command not found Instead of command does not work. Does anyone know why it's not printing my error…
Cinder
  • 1,599
  • 2
  • 13
  • 23
38
votes
2 answers

What's wrong with my except?

I've got a SyntaxError on my except: try: opts, args = getopt.getopt(sys.argv[1:], 'P:D:H:d:u:p:nvhmJi:c:Ml:TB:', ['host=', 'port=', 'directory=', 'user=', 'password=', 'daemon=', 'noauth', 'help', 'verbose', 'mysql', …
lagarkane
  • 915
  • 2
  • 9
  • 22
38
votes
3 answers

How does LINQ Except work?

Possible Duplicate: LINQ find differences in two lists I want to find a difference between 2 series. So I am using Except in the LINQ statement. But Except seems to work only when the first collection is longer than the second. For example this…
gunnerz
  • 1,898
  • 5
  • 24
  • 39
26
votes
7 answers

C# Linq intersect/except with one part of object

I've got a class: class ThisClass { private string a {get; set;} private string b {get; set;} } I would like to use the Intersect and Except methods of Linq, i.e.: private List foo = new List(); private List bar…
David Archer
  • 2,008
  • 4
  • 26
  • 31
20
votes
5 answers

Invalid syntax (SyntaxError) in except handler when using comma

I have this code: @app.route('/login/', methods=['GET', 'POST']) def login(): error = None if request.method == 'POST': session['username'] = request.form['username'] session['password'] = request.form['password'] …
user950779
19
votes
5 answers

Using Linq Except not Working as I Thought

List1 contains items { A, B } and List2 contains items { A, B, C }. What I need is to be returned { C } when I use Except Linq extension. Instead I get returned { A, B } and if I flip the lists around in my expression the result is { A, B, C }. Am…
Schanckopotamus
  • 781
  • 2
  • 6
  • 20
14
votes
3 answers

How do I select all inputs except under a specific id?

What I want to do is to select all the inputs buttons on the document, except those that reside under a specific id. Example:
Jose A
  • 10,053
  • 11
  • 75
  • 108
13
votes
5 answers

Repetitive Try and Except Clauses

I've created a bunch of functions and I need very similar except clauses in all of them, but I hate having so many lines of try and except clauses and the same code inside of each function. For example: import sys import random def foo(): …
crunkchitis
  • 718
  • 2
  • 10
  • 19
12
votes
3 answers

Compare two SQL tables and return missing ids?

I have two simple tables: (here only the "id" column) table1: id 1 2 3 4 table2: id 2 4 the sql query should compare the two tables for missing "id" in table2 and return: 1,3 any ideas? :) TY
MilMike
  • 12,571
  • 15
  • 65
  • 82
1
2 3
39 40