简体   繁体   English

尝试/捕获/异常

[英]bluej Try/catch/exception

I'l post my three question where the questions occur aswell in the code, aswell as post it up here. 我将在代码中也出现问题的地方张贴我的三个问题,并将其张贴在这里。

Q 1 what are threads and how do i use them? Q 1什么是线程,我该如何使用它们?
Q 2 how do 'try' / 'catch' work? Q 2“尝试” /“捕获”如何工作? Q 3 what is the exception? Q 3有什么例外? see code. 参见代码。

Thank you in advance :) 先感谢您 :)

public void start() {
  //  Question 1: What are these threads and how do i implement them?
  Thread frameTiming = new Thread (this);
  frameTiming.start ();
}

public void run() {
  while (true) {
    xleft-=10;
    yleft-=10;
    xleft2+=10;
    yleft2+=10;
    i-=5;
    repaint();
    //  Question 2: what does try/catch do?
    try {
      Thread.sleep(40);
    // question3: what does this exception do exactly? :0
    } catch (Exception e) {
    }

}

Threads are objects that contain enough support structures to act as a lightweight process. 线程是包含足够支持结构以充当轻量级进程的对象。

The process is like a program that is executing, but it is a formalized definition, and is the thing(s) managed by the operating system, which decides if a process gets CPU time or not. 进程就像正在执行的程序一样,但是它是形式化的定义,是由操作系统管理的事物,它决定进程是否获得CPU时间。

The reason a thread is like a lightweight process is because even though it gets CPU time allocated almost independently of the main program, it doesn't get everything a process gets. 线程之所以像轻量级进程,是因为即使它几乎独立于主程序分配CPU时间,也无法获得进程获得的所有信息。 It shares the memory with the other threads in the process, while real processes get their own private memory spaces. 它与进程中的其他线程共享内存,而实际进程则拥有自己的私有内存空间。

The try / catch block you've added is to catch a known, declared exception that it thrown when you call Thread.sleep(...) . 您添加的try / catch块是捕获一个已知的,声明的异常,该异常在您调用Thread.sleep(...)时引发。 Since the thread is asking to be taken off of the CPU for at least 40 milliseconds, there is a chance that while it is off of the CPU, the program shutsdown, or some other Thread tells it to "wake" up for some other reason. 由于该线程被要求从CPU上离开至少40毫秒,因此有可能在该线程从CPU上退出时,程序关闭,或者某些其他线程告诉它由于某些其他原因而“唤醒” 。 If this happens, then the Thread.sleep(...) call will not finish the sleep normally, but instead will terminate the call abnormally with an InterruptedException 如果发生这种情况,则Thread.sleep(...)调用将无法正常完成睡眠,而是会使用InterruptedException异常终止该调用。

Q 1 what are threads and how do i use them? Q 1什么是线程,我该如何使用它们?

When you run code it usually all runs linearly. 运行代码时,通常所有代码都是线性运行的。 Suppose you ask for user input. 假设您要求用户输入。 The program stops and then awaits user input. 程序停止,然后等待用户输入。 What if the user never inputs? 如果用户从不输入怎么办? The program never moves on. 该程序永远不会继续进行。 Having multiple threads allows you to do things in the background while you wait for user input. 具有多个线程可让您在等待用户输入时在后台执行操作。 It also allows you to divide work up into multiple simultaneous methods to take advantage of multiple cores, although without the proper algorithm to divide the work you wouldn't be doing yourself much of a favor. 它还允许您将工作划分为多个同时的方法,以利用多个内核,尽管如果没有适当的算法来划分工作,您将不会大有帮助。

How to start a thread in Java 如何在Java中启动线程

Q 2 how do 'try' / 'catch' work? Q 2“尝试” /“捕获”如何工作?

In Java and many other languages try and catch is how you handle possible errors that usually you believe you can recover from. 在Java和许多其他语言中,尝试并捕捉是如何处理通常认为可以从中恢复的可能错误。 For example if you ask a user for a number it is possible he will give you a letter, therefore you don't ask the user for a number you TRY to ask the user for a number. 例如,如果您问一个用户一个数字,则有可能他会给您一封信,因此,您不会要求该用户输入一个数字,而请尝试向该用户输入一个数字。 If the user makes a mistake you CATCH his mistake and then make recourse. 如果用户犯了一个错误,您可以抓住他的错误,然后提出追索权。 This doesn't just apply to user input suppose you open a file but it does not exist or some other error occurs which causes the read operation to fail. 假设您打开了一个文件,但这并不仅仅适用于用户输入,但它不存在,或者发生了其他错误导致读取操作失败。 This possibility requires you to TRY it rather than go ahead and just do it. 这种可能性要求您尝试一下,而不是继续进行。

How to use try/catch 如何使用try / catch

Q 3 what is the exception? Q 3有什么例外?

An exception is something a method throws when something has gone wrong. 异常是发生错误时方法抛出的错误。 In Java there is two types of exceptions, checked and unchecked. 在Java中,有两种类型的异常,已检查和未检查。 Most unchecked exceptions are due to programmer error such as array is out of bounds. 大多数未经检查的异常是由于程序员错误引起的,例如数组超出范围。 However the common exceptions such as trying to open a file but you can't because it does not exist, these Exceptions are called checked exceptions meaning you HAVE TO surround them with a try catch, this is pretty unique to Java. 但是,常见的异常(例如尝试打开文件但由于不存在而无法打开)等异常称为被检查异常,这意味着您必须用try catch对其进行包围,这是Java特有的。

Click here to learn in a more professional manner the three types of Exceptions in Java 单击此处以更专业的方式学习Java中的三种异常类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM