exception-handling

Python socket connection exception

强颜欢笑 提交于 2019-12-30 01:09:07
问题 I have a socket-connection going on and I wanna improve the exception handling and Im stuck. Whenever I use the socket.connect(server_address) function with an invalid argument the program stops, but doesnt seem to throw any exceptions. Heres my code import socket import sys import struct class ARToolkit(): def __init__(self): self.x = 0 self.y = 0 self.z = 0 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.logging = False def connect(self,server_address): try: self.sock

Python try/except: Showing the cause of the error after displaying my variables

不羁的心 提交于 2019-12-29 16:34:31
问题 I'm not even sure what the right words are to search for. I want to display parts of the error object in an except block (similar to the err object in VBScript, which has Err.Number and Err.Description). For example, I want to show the values of my variables, then show the exact error. Clearly, I am causing a divided-by-zero error below, but how can I print that fact? try: x = 0 y = 1 z = y / x z = z + 1 print "z=%d" % (z) except: print "Values at Exception: x=%d y=%d " % (x,y) print "The

Purpose of Java Exception Handling

青春壹個敷衍的年華 提交于 2019-12-29 09:57:47
问题 I came across below doubts while learning java exception handling, Most(nearly all) of the example i've seen thows an exception using try bock and the appropriate catch prints a message to the user in the console and finally block used to close/release resources. Is exception handling is only about printing the message/cause for exception? While learning arithmetic exception the only example i got is DividebyZero scenario, is there any other operation that can cause arithmetic exception? 回答1:

Infinite While Loop When InputMidmatchException is caught in try-catch block [duplicate]

不羁岁月 提交于 2019-12-29 08:55:35
问题 This question already has answers here : try/catch with InputMismatchException creates infinite loop (7 answers) Closed 4 years ago . I keep getting my code caught in an infinite while loop. It is nothing to advanced, but i can not figure it out for the life of me! Someone Please help I have purplosely just re created the specific error without all of the if statements i have in my actual program. package bs; import java.util.InputMismatchException; import java.util.Scanner; public class bs {

FaultException and custom exception WCF

≡放荡痞女 提交于 2019-12-29 07:55:11
问题 I have a question on how to send a custom exception as FaultException. It works when I use a system Exception like ArgumentException, but if I change it to my custom exception "TestException" it fails. I can’t get the configuration for the service reference, when I try to add it. Works: [OperationContract] [FaultContract(typeof(ArgumentException))] [TransportChannel TestMethod (); public Void TestMethod() { throw new FaultException<ArgumentException>(new ArgumentException("test"), new

How to better understand the code/statements from “Async - Handling multiple Exceptions” article?

我只是一个虾纸丫 提交于 2019-12-29 07:17:09
问题 Running the following C# console app class Program { static void Main(string[] args) { Tst(); Console.ReadLine(); } async static Task Tst() { try { await Task.Factory.StartNew (() => { Task.Factory.StartNew (() => { throw new NullReferenceException(); } , TaskCreationOptions.AttachedToParent ); Task.Factory.StartNew ( () => { throw new ArgumentException(); } ,TaskCreationOptions.AttachedToParent ); } ); } catch (AggregateException ex) { // this catch will never be target Console.WriteLine("**

How do you guard for Null Reference exceptions in Linq To Xml?

僤鯓⒐⒋嵵緔 提交于 2019-12-29 06:44:44
问题 <?xml version="1.0" encoding="utf-8" ?> <pages> <page id="56"> <img id="teaser" src="img/teaser_company.png"></img> </page> </pages> I have an xml file that defines additional resources for pages within a cms. What's the best way to guard for Null Reference exceptions when querying this file with LinqToXml? var page = (from tabElement in extensionsDoc.Descendants("page") where tabElement.Attribute("id").Value == tabId.ToString() select tabElement).SingleOrDefault(); This code could

Throw Exception inside a Task - “await” vs Wait()

℡╲_俬逩灬. 提交于 2019-12-29 06:41:11
问题 static async void Main(string[] args) { Task t = new Task(() => { throw new Exception(); }); try { t.Start(); t.Wait(); } catch (AggregateException e) { // When waiting on the task, an AggregateException is thrown. } try { t.Start(); await t; } catch (Exception e) { // When awating on the task, the exception itself is thrown. // in this case a regular Exception. } } In TPL, When throwing an exception inside a Task, it's wrapped with an AggregateException. But the same is not happening when

Throw Exception inside a Task - “await” vs Wait()

南楼画角 提交于 2019-12-29 06:41:02
问题 static async void Main(string[] args) { Task t = new Task(() => { throw new Exception(); }); try { t.Start(); t.Wait(); } catch (AggregateException e) { // When waiting on the task, an AggregateException is thrown. } try { t.Start(); await t; } catch (Exception e) { // When awating on the task, the exception itself is thrown. // in this case a regular Exception. } } In TPL, When throwing an exception inside a Task, it's wrapped with an AggregateException. But the same is not happening when

Python BaseHTTPServer, how do I catch/trap “broken pipe” errors?

微笑、不失礼 提交于 2019-12-29 05:57:47
问题 I build a short url translator engine in Python, and I'm seeing a TON of "broken pipe" errors, and I'm curious how to trap it best when using the BaseHTTPServer classes. This isn't the entire code, but gives you an idea of what I'm doing so far: from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer import memcache class clientThread(BaseHTTPRequestHandler): def do_GET(self): content = None http_code,response_txt,long_url = \ self.ag_trans_url(self.path,content,'GET') self.http_output(