标签: thread-exceptions

如何从java线程中抛出一个检查过的异常?

嘿,我正在写一个网络应用程序,我在其中读取一些自定义二进制格式的数据包.我正在开始一个后台线程来等待传入的数据.问题是,编译器不允许我将任何代码抛出(检查)异常run().它说:

run() in (...).Listener cannot implement run() in java.lang.Runnable; overridden method does not throw java.io.IOException

我希望异常杀死该线程,并让它在父线程的某处被捕获.这是可能实现还是我必须处理线程的每个异常?

java exception thread-exceptions

40
推荐指数
5
解决办法
7万
查看次数

评估需要一个线程暂时运行.使用"监视"窗口执行评估

完全卡住了.我正在测试MetaTrader API并在尝试在VS 2010的立即窗口中运行方法时获取下一个错误:

mscorlib.dll中出现'System.Threading.ThreadAbortException'类型的第一次机会异常

System.Runtime.Remoting.dll中出现'System.Threading.ThreadAbortException'类型的第一次机会异常

评估需要一个线程暂时运行.使用"监视"窗口执行评估.

这是什么意思?是否会因运行时版本的差异而发生(api 2.0,app 4.0)?

c# exception-handling thread-exceptions metatrader4

12
推荐指数
3
解决办法
1万
查看次数

为什么AppDomain异常总是终止应用程序?

这与之前的问题有关.

我现在想要了解的是如何防止UI线程异常终止应用程序,而非UI异常不能.

供参考,请参阅此示例.

最重要的是,在这种情况下我希望能够"静默地"终止进程 - 不显示Windows对话框,询问我是否要发送错误报告.

这是我的AppDomain UnhandledExceptionHandler:

private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{            
    try
    {
        // Maybe do some logging here if allowed
    }
    catch
    {
    }

    // then just terminate the application
    Application.Exit();            
}
Run Code Online (Sandbox Code Playgroud)

更新
根据本回答中的评论,我想澄清一点,最重要的是,我想了解更多关于使UI线程尽早通过该Application.ThreadException机制捕获未处理异常的机制.是否可以在非UI线程上实现此类行为.

.net exception-handling thread-exceptions

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

Android:引起:android.os.NetworkOnMainThreadException

String response = getResultForRequest(url);
Run Code Online (Sandbox Code Playgroud)

其中'url'是JSON格式的,使用http GET方法返回数据束.

public static String getResultForRequest(String urlString)
        throws IOException {
    URL url = new URL(urlString);
    HttpURLConnection urlConnection = (HttpURLConnection) url
            .openConnection();
    urlConnection.setRequestMethod("GET");
    urlConnection.setDoInput(true);
    urlConnection.setDoOutput(true);
    urlConnection.connect();

    InputStream is = urlConnection.getInputStream();
    if (is == null)
        return null;
    StringBuffer sb = new StringBuffer();
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    String line = null;
    try {
        while ((line = br.readLine()) != null)
            sb.append(line);
    } finally {
        br.close();
        is.close();
    }

    return sb.toString();
}
Run Code Online (Sandbox Code Playgroud)

我无法从我在getResultForRequest(url)方法中传递的'url'中获取JSON格式的数据.我在urlConnection.connect()遇到错误; .AndroidManifest.xml文件中也提供了Internet权限.

这是我的日志. …

android thread-exceptions

7
推荐指数
2
解决办法
5万
查看次数

“thread._local”对象没有属性

我试图通过添加上下文过滤器来更改日志记录格式。我的格式是这样的

FORMAT = "%(asctime)s %(VAL)s %(message)s"
Run Code Online (Sandbox Code Playgroud)

这是我用来在格式中设置 VAL 的类。

class TEST:
  def __init__(self, val):
    self.test_var=threading.local()
    self.test_var.value=val
  def filter(self,record):
    record.VAL=self.test_var.value
    return True
  def setValue(self,val)
    self.test_var.value=CMDID
Run Code Online (Sandbox Code Playgroud)

它在单线程环境中工作正常,但对于某些多线程环境,我收到错误

<Fault 1: "exceptions.AttributeError:'thread._local' object has no attribute 'value'">
Run Code Online (Sandbox Code Playgroud)

谁能告诉我这里有什么问题??以及如何纠正?

python logging multithreading thread-exceptions

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

异步更新视图

我正在尝试recyclerview使用我要异步获取的来自网络的数据填充。

我有一个函数loadData(),该函数onCreateView()首先使加载指示器可见,然后调用suspend函数加载数据,然后尝试通知视图适配器进行更新。

但是在这一点上,我得到以下异常:

android.view.ViewRootImpl $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触摸其视图。

令我感到惊讶的是,我的理解是只有get_top_books()在不同的线程上调用了我的函数,而以前在显示加载指示器时,我显然是在正确的线程上。

那么,为什么会引发此运行时异常?

我的代码:

class DiscoverFragment: Fragment() {

    lateinit var loadingIndicator: TextView
    lateinit var viewAdapter: ViewAdapter
    var books = Books(arrayOf<String>("no books"), arrayOf<String>("no books"), arrayOf<String>("no books"))

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val viewFrame = layoutInflater?.inflate(R.layout.fragment_discover, container, false)

        val viewManager = GridLayoutManager(viewFrame!!.context, 2)
        viewAdapter = ViewAdapter(books)
        loadingIndicator = viewFrame.findViewById<TextView>(R.id.loading_indicator)

        val pxSpacing = (viewFrame.context.resources.displayMetrics.density * 8f + .5f).toInt()

        val recyclerView = viewFrame.findViewById<RecyclerView>(R.id.recycler).apply {
            setHasFixedSize(true)
            layoutManager …
Run Code Online (Sandbox Code Playgroud)

android thread-exceptions kotlin kotlin-coroutines

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

下面的代码运行成功,这是否意味着我们可以启动线程两次?

下面的代码运行成功,这是否意味着我们可以启动线程两次?

public class enu extends Thread {
    static int count = 0;

    public void run(){
        System.out.println("running count "+count++);
    }

    public static void main(String[] args) {
        enu obj = new enu();
        obj.run();
        obj.start();
    }
}
Run Code Online (Sandbox Code Playgroud)

输出 - 运行计数0运行计数1

java multithreading thread-exceptions

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

使用AsyncTask从错误的线程异常调用

即使我把它扔进AsyncTask的DoInBackground方法,我仍然会得到一个CalledFromWrongThreadException!任何人都可以告诉我为什么以及如何解决它?

/**
     * Recognizes key words in responseTexts and runs the appropriate function
     * @param responseText
     */
    private void responseHandler(String responseText) {
        if(responseText.startsWith("Notes", 0)) {
            notes.add(responseText);
            new DownloadFilesTask().execute("WriteInternal" , null, null);
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是我的AsyncTask类

private class DownloadFilesTask extends AsyncTask<String, Void, String> {
    protected String doInBackground(String... command) {
    if(command[0].equals("WriteInternal") {
        //write to internal storage
    FileOutputStream fOut = null;
    OutputStreamWriter osw = null;
    try{
        fOut = openFileOutput("notes.txt", Context.MODE_PRIVATE);
        osw = new OutputStreamWriter(fOut);
        for (int i = 0; i < notes.size(); i++){
            osw.write(notes.get(i) …
Run Code Online (Sandbox Code Playgroud)

android asyncsocket thread-exceptions android-asynctask

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

Thread.IsBackground抛出ThreadStateException

下一段代码抛出一个ThreadStateException:

public void StartListening()
{
     this.isListening = true;
     if (!this.listeningThread.IsAlive)
         this.listeningThread = new Thread(ListenForClients);
     this.listeningThread.Start();
     this.listeningThread.IsBackground = true;
}
Run Code Online (Sandbox Code Playgroud)

并设置IsBackground属性

this.listeningThread.IsBackground = true;
Run Code Online (Sandbox Code Playgroud)

抛出异常.

怎么了?我在错误的地方使用IsBackground = true吗?

例外文字:

线程死了; 国家无法访问.
at System.Threading.Thread.SetBackgroundNative(Boolean isBackground
)
at My Systemspace.MyClass.StartListening()的System.Threading.Thread.set_IsBackgrounf(布尔值)
...

IsBackground属性仅在一个地方设置,此处.因此,它在线程工作期间永远不会改变.不幸的是我不能重现这个(仅在客户的系统上复制),所以我不知道原因.这就是我要问的原因.

c# multithreading thread-exceptions

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