我正在编写一个小插件库,它使用app域来隔离使用.Net framework 4.0的插件.因此,每个插件中的代码都不受我的控制.当其中一个插件出现未处理的异常时,我发现结果是混合包.它们如下.
当未处理的异常被插入到插件的主线程中时,调用插件的execute方法的主要可插拔应用程序能够清楚地捕获并处理它.没有问题.然而,
如果插件在插件的Execute方法中启动基于WinForms的应用程序的消息循环,并且在WinForm应用程序中抛出未处理的异常(即在表单中),那么可插拔应用程序只能在Visual Studio调试器内部运行时捕获异常.否则(在VS外部调用时)主插件应用程序与插件一起崩溃.
如果在插件的Execute方法产生的单独线程中抛出未处理的异常,则可插拔应用程序无法捕获异常并且崩溃.
我创建了一个简单的VS 2010项目,以在下面的链接中模拟此行为. http://www.mediafire.com/file/1af3q7tzl68cx1p/PluginExceptionTest.zip
在其中,可插拔应用程序中的主要方法如下所示
namespace PluginExceptionTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Press enter to load plugin");
Console.ReadLine();
Assembly entryAsm = Assembly.GetEntryAssembly();
string assemblyFileName = Path.Combine(Path.GetDirectoryName(entryAsm.Location), "EvilPlugin.exe");
AppDomainSetup domainSetup = new AppDomainSetup();
AppDomain domain = AppDomain.CreateDomain("PluginDomain", null, domainSetup);
PluginBase plugin = (PluginBase)domain.CreateInstanceFromAndUnwrap(assemblyFileName, "EvilPlugin.Plugin");
Console.WriteLine("Plugin Loaded.");
//SCENARIO 1: WinForms based plugin
Console.WriteLine("Press Enter to execute winforms plugin. (Remember to click on the button in the form to raise exception)");
Console.ReadLine();
try …Run Code Online (Sandbox Code Playgroud) 如何处理ASP.NET应用程序处理非请求后台线程上发生的未处理异常(由于错误)?
默认情况下,此类异常会导致进程终止.这在设置ASP.NET工作进程时是不可接受的,因为并发运行的请求会无法预料地中止.这也是一个性能问题.
请求线程上的异常不是问题,因为ASP.NET处理它们(通过显示错误页面).
该AppDomain.UnhandledException事件允许观察发生了异常,但此时无法阻止终止.
这是一个需要粘贴到ASPX页面代码隐藏的repro.
protected void Page_Load(object sender, EventArgs e)
{
var thread = new Thread(() =>
{
throw new InvalidOperationException("some failure on a helper thread");
});
thread.Start();
thread.Join();
}
Run Code Online (Sandbox Code Playgroud)
我所知道的唯一解决方案是永远不要让异常"逃避"未处理.对此还有其他更全面彻底的解决方案吗?
(这看起来非常类似于来自另一个线程的C#UnhandledException保持循环,但我不是试图在这里捕获异常,只是有机会记录一些东西)
我有一些非常简单的C#代码,用于设置UnhandledException事件处理程序,然后抛出异常:
class Program
{
static void Main(string[] args)
{
AppDomain currentDomain = AppDomain.CurrentDomain;
//currentDomain.UnhandledException += new UnhandledExceptionEventHandler(currentDomain_UnhandledException);
currentDomain.UnhandledException += (sender, eventArgs) =>
{
var exception = (Exception) eventArgs.ExceptionObject;
Console.WriteLine("Unhandled exception: " + exception.Message);
};
throw new AccessViolationException("Bleurgh");
}
}
Run Code Online (Sandbox Code Playgroud)
它的行为与我期望从控制台一样:
Unhandled exception: Bleurgh
Unhandled Exception: System.AccessViolationException: Bleurgh
at UnhandledExceptions.Program.Main(String[] args) in c:\code\sandbox\UnhandledExceptions\UnhandledExceptions\Program.cs:line 20
Run Code Online (Sandbox Code Playgroud)
但是当我尝试在Visual Studio中调试它时,它进入一个循环,进入事件处理程序,然后退出以重新抛出异常.
当我将处理程序表示为一个独特的静态方法时,会发生同样的事情.
有什么想法发生了什么?
这是在Visual Studio 2010中.编辑:和.NET 4.
我尝试基于回答实现解决方案如何处理单元测试时在其他线程中引发的异常?,但我仍然不明白在处理程序中要做什么.我们假设我有一个测试:
[TestMethod]
void Test()
{
new Thread(() => { throw new Exception(); }).Start();
}
Run Code Online (Sandbox Code Playgroud)
我有和所有测试的全局初始化:
[AssemblyInitialize]
public static void AssemblyInitialize(TestContext context)
{
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += currentDomain_UnhandledException;
}
static void currentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Exception ex = e.ExceptionObject as Exception;
if (ex != null)
Trace.WriteLine(ex);
Assert.Fail("Unhandled Exception in thread.");
}
Run Code Online (Sandbox Code Playgroud)
问题是Assert.Fail实际上抛出异常,它再次被currentDomain_UnhandledException捕获并导致MSTest崩溃(stackoverflow?).我不想捕获Assert.Fail,但我想让测试失败.怎么解决?
我知道我可以捕获异常并在测试的主线程上调用它,但我需要全局解决方案进行数千次测试.我不想让每一个测试都复杂化.
我以前从来没有得过这个错误所以我不知道该做什么或它意味着什么
未处理的异常类型
OperationApplicationException
它出现在这段代码中:
public void putSettings(SharedPreferences pref){
ArrayList<ContentProviderOperation> ops =
new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
.withSelection(Data.RAW_CONTACT_ID + "=?", new String[]{String.valueOf(pref.getString(SmsPrefs.ID, ""))})
.withValue(Data.MIMETYPE,"vnd.android.cursor.item/color")
.withValue("data1",nColor).build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); //error
ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
.withSelection(Data.RAW_CONTACT_ID + "=?", new String[]{String.valueOf(pref.getString(SmsPrefs.ID, ""))})
.withValue(Data.MIMETYPE,"vnd.android.cursor.item/vibrate")
.withValue("data1", nVibrate).build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); //error
ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
.withSelection(Data.RAW_CONTACT_ID + "=?", new String[]{String.valueOf(pref.getString(SmsPrefs.ID, ""))})
.withValue(Data.MIMETYPE, "vnd.android.cursor.item/sound")
.withValue("data1", ringTonePath).build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);//error
}
Run Code Online (Sandbox Code Playgroud)
它给了我2个选项"添加抛出声明"和"环绕尝试/捕获".
我该怎么办?为什么?
我知道在使用Tasks 时如何处理无法处理的异常,如果用户代码尚未"观察"它,则仅在终结器中抛出未处理的异常.
我也知道如何Action.BeginInvoke()在加入调用中捕获并重新抛出异步线程(例如)中的未处理异常(例如Action.EndInvoke()).
我不明白的是,这不会导致这个过程崩溃的原因是什么?
static void Main(string[] args)
{
var timer = new System.Timers.Timer() {Interval = 100};
timer.Elapsed += (o, e) => { throw new Exception(); };
timer.Start();
Console.ReadKey( true );
}
Run Code Online (Sandbox Code Playgroud) 使用:MSVS2012
码
elemalg.h
#include <vector>
#include <string>
#include <fstream>
class ElemAlg
{
private:
std::string difficultlyLevel, question, answerToRead;
std::vector<std::string> questions, answers;
std::vector<std::string> GetQuiz(int);
};
Run Code Online (Sandbox Code Playgroud)
elemalg.cpp
#include "elemalg.h"
std::vector<std::string> ElemAlg::GetQuiz(int difficulty)
{
if (difficulty == 1) { difficultyLevel = "algE"; }
if (difficulty == 2) { difficultyLevel = "algM"; }
if (difficulty == 3) { difficultyLevel = "algH"; }
if (difficulty == 4) { difficultyLevel = "algVH"; }
std::ifstream fin(difficultyLevel + ".txt");
while (std::getline(fin, question)) { questions.push_back(question); }
fin.close();
std::ifstream fin2(difficultyLevel + …Run Code Online (Sandbox Code Playgroud) 我正在研究UWP应用程序.我正在使用异步/等待很多.总是在我的代码中发生异常时,调试器在App.gics中设置中断
#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
UnhandledException += (sender, e) =>
{
if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
};
#endif
Run Code Online (Sandbox Code Playgroud)
但我希望在发生异常时看到该行.如何实现这种行为?
我是新手,已经成功安装它并在一台机器上工作,但是在这台机器上工作时,我遇到了一些问题。这是android studio,flutter和gradle的全新安装。机器正在运行更新的Windows 10。
Flutter Doctor语句在检索设备属性时返回错误。
C:\Development\FlutterApps\first_app>flutter doctor -v
[?] Flutter (Channel stable, v1.0.0, on Microsoft Windows [Version 10.0.17134.523], locale en-US)
• Flutter version 1.0.0 at C:\Development\Flutter
• Framework revision 5391447fae (8 weeks ago), 2018-11-29 19:41:26 -0800
• Engine revision 7375a0f414
• Dart version 2.1.0 (build 2.1.0-dev.9.4 f9ebf21297)
\Error retrieving device properties for ro.product.cpu.abi:
[
?] Android toolchain - develop for Android devices (Android SDK 28.0.3)
• Android SDK at C:\Development\Android
• Android NDK location not configured (optional; useful for native …Run Code Online (Sandbox Code Playgroud) 我正在尝试从互联网上获取一些数据。所以我为一个天气网站做了一个 API 请求。但我收到以下异常 - 未处理的异常:FormatException: Invalid radix-10 number (at character 1)。这是错误代码:
[VERBOSE-2:shell.cc(242)] Dart Unhandled Exception: FormatException: Invalid radix-10 number (at character 1)
//uri.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=4659036c3236...
^
, stack trace: #0 int._throwFormatException (dart:core-patch/integers_patch.dart:131:5)
#1 int._parseRadix (dart:core-patch/integers_patch.dart:157:16)
#2 int._parse (dart:core-patch/integers_patch.dart:100:12)
#3 int.parse (dart:core-patch/integers_patch.dart:63:12)
#4 _Uri._makeHttpUri (dart:core/uri.dart:1591:49)
#5 new _Uri.https (dart:core/uri.dart:1462:12)
#6 _LoadingScreenState.getData (package:clima/screens/loading_screen.dart:25:49)
#7 _LoadingScreenState.build (package:clima/screens/loading_screen.dart:37:5)
#8 StatefulElement.build (package:flutter/src/widgets/framework.dart:4612:27)
#9 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4495:15)
#10 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4<…>
Run Code Online (Sandbox Code Playgroud)
这是与之相关的代码:
import 'package:flutter/material.dart';
import 'package:clima/services/location.dart';
import 'package:http/http.dart' as http;
class LoadingScreen extends StatefulWidget {
@override
_LoadingScreenState createState() => _LoadingScreenState(); …Run Code Online (Sandbox Code Playgroud)