Questions tagged [try-except]

A form of error handling in Python and Delphi, similar to try/catch in C-like languages. It is also a Microsoft extension in C and C++.

Try/Except is a form of error handling in Python and Delphi, similar to try/catch in C-like languages. It is also a Microsoft extension in C and C++.

Links

Error Handling in Python
Except Keyword in Delphi
Try/Except in Microsoft C and C++

898 questions
-2
votes
1 answer

Error using exception handling in python

I just started learning python, while using the try command in exception handling, I got an syntax error Here is my code, def divide(a,b): { print(a," hello world", b) try: return a/b except: …
-2
votes
4 answers

What's wrong with this try/ except block in Python?

try: user_name = str(input("Enter your full name: ")) except: print("Enter a string") user_age = int(input("Enter your age: ")) user_country = str(input("Enter the country you live in: ")) user_postcode =…
-2
votes
1 answer

Try except in file opening

I wrote a program to open a file and print every single line if the file is not there create it and write some line to it it works perfectly when the file is not present in the directory but when the file is there it opens the file reading it…
Geofe Geo
  • 15
  • 3
-2
votes
1 answer

Invalid datetime error

I am using datefinder module to find date references in the text. However, the generator returned by the function call might return invalid dates. How to tackle this using a try-except block? import datefinder file = open('abc.txt', 'r') for…
-2
votes
2 answers

Is there an alternative for sys.exit() in python?

try: x="blaabla" y="nnlfa" if x!=y: sys.exit() else: print("Error!") except Exception: print(Exception) I'm not asking about why it is throwing an error. I know that it raises exceptions.SystemExit. I was wondering if…
kragor
  • 105
  • 2
  • 11
-2
votes
3 answers

Datetime module - ValueError try/except won't work python 3

I have a programming homework assignment. Everything went smoothly until I reached a problem using Try/Except. If I type a valid datetime, the program will take it and it will move on, but if I use a valid datetime format, the exception won't…
tokyolerd
  • 29
  • 1
  • 10
-2
votes
1 answer

In Python, how does one try (and except) the instantiation of a class?

In Python, how does one try/except the instantiation of a class? For example, I'm working on a GitHub script at the moment: from github3 import login user = login(username, password) At first, I thought it would be as easy as: try: user =…
JoeNyland
  • 280
  • 2
  • 22
-3
votes
1 answer

"If at least one of these keys is not in the dictionary, the function should generate a TypeError error." How to do it?

def image_info(**o): print(type(o)) print(o) { 'image_title': 'my_cat', 'image_id': 1234 } info = ( f"\"Image '{o['image_title']}'" f" has id {o['image_id']}" ) return info info =…
-3
votes
1 answer

【Django.View】Why does function disable 「if」 or 「try/except」

My trouble is the following code CreateView.py # Add new function def error_display(self, message) context={ 'message':message, } return render(self.request, 'error.html', context) #I modified form_valide function def…
Genzo Ito
  • 189
  • 11
-3
votes
1 answer

Why exception (try-except Exception) does not work?

Here is the code. For some reason, if I have type_of_model, neitehr X, nor Y exception does not work. The exception does not appear. `def preprocess_corresponds_to_model(type_of_model: str) -> function: try: if type_of_model == "X": …
Ololo
  • 11
  • 3
-3
votes
1 answer

Problem with .split() my program miscounts a certain input

We were tasked to create a program that counts the total numbers of unique words from a user input and we are not allowed to use sets and dictionaries. Words with duplicates or identical words should not be counted. I have a code and it works with…
-3
votes
2 answers

Why does the try-except not work when the variable input is given as a float?

I've been trying to work out why how the try-except statement works out but became stuck on the following code. The input will be Enter Hours: 10 and Enter Rate: ten sh = float(input("Enter Hours: ")) sr = float(input("Enter Rate: ")) try: fh =…
Learner_15
  • 399
  • 2
  • 11
-3
votes
1 answer

Weird behavior in try.. except

I'm trying to write a function that takes three arguments: a list of lists and two indexes i and j that should be both positive. The function should return True if the corresponding element exists within the list and return False otherwise. My idea…
vd743r
  • 13
  • 2
-3
votes
2 answers

How to make try ~ except in Python?

Still Python is a difficult language for me..T_T I really need your help. I'm trying to crawl some website. The website URL has four digits at the end as shown below. URL → http://www.boanjob.co.kr/work/employ_detail.html?no=**2196** So I composed…
MarkPact
  • 17
  • 2
-3
votes
2 answers

How can I break the while loop and go to the finally block if the user entered "done"

largest = None smallest = None l = [] while True: try: num = input("Enter a number: ") except NameError as err: if err == "done": break else: print("Invalid input") finally: …
1 2 3
59
60