Tag: try catch

我试图添加一个尝试捕获,告诉用户他们不能插入负数

我能够添加一个尝试捕获,告诉用户他们不能使用字母。但是由于某种原因添加一个尝试捕获负数量dosent似乎工作。我知道try块是在哪里,如果somthing可能会出错像进入一个负数,catch可以打印出错误信息。 我认为这就是我的问题所在。 与try catch相关的另一个问题是我使用输入-1的用户输入用户输入的内容,所以我认为它会导致逻辑问题。 tl; dr添加try catch或其他catch以防止用户添加负数 这不是整个程序,但它的作用是过滤掉用户输入的整数并分离平均值和赔率。 public static void main(String [] args) { Scanner stdin = new Scanner(System.in);//for user input int[] evenNum = new int [100];//Even Array up too 100 int[] oddNum = new int[100];//Odd Array up too 100 int evenIndex=0;//even numbers int input=0;//user input int i=0;//incrementer for arrays int k=0; int j=0; […]

尝试并在android中捕获错误

我正在开发一个在片段中实现Maps的Android应用程序。 但是我遇到了try和catch块的问题。 我一直收到错误Exception ‘com.google.android.gms.common.GooglePlayServicesNotAvailableException ‘永远不会在相应的try块中抛出。 我该如何解决这个错误? 提前致谢。 这是代码: import android.support.v4.app.Fragment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import com.google.android.gms.common.GooglePlayServicesNotAvailableException; import com.google.android.gms.maps.CameraUpdate; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.MapView; import com.google.android.gms.maps.MapsInitializer; import com.google.android.gms.maps.model.LatLng; public class MapActivity extends Fragment { MapView mapView; GoogleMap map; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, […]

尝试catch没有捕获错误的数字输入(也得到java.lang.arithmaticexception/零)

这是我用于测试平均类的构造函数。 我的任务要求我引入一个测试分数列表并作为输入validation,它希望我使用try catch语句来捕获0和100以上的任何输入。 下面的构造函数是从我的main引入一个数组列表,没有任何错误。 然而,它没有捕获负面的输入。 我一直在看这个代码超过两个小时,我无法弄明白。 我认为一双新鲜的眼睛可能会明白为什么它没有抓住糟糕的输入。 我的整个计划: 类: import javax.swing.*; import java.util.*; class try2 { public static ArrayListuserInput=new ArrayList(); public static double avg; public try2() { } public try2(ArrayList test) { for ( int x = 0 ; x <= test.size(); x++) { try { if ( test.get(x) 100) { throw new IllegalArgumentException (); […]

java try catch块的特殊语法

Charset charset = Charset.forName(“US-ASCII”); try (BufferedReader reader = Files.newBufferedReader(file, charset)) { String line = null; while ((line = reader.readLine()) != null) { System.out.println(line); } } catch (IOException x) { System.err.format(“IOException: %s%n”, x); } 我在The Java Tutorials上看到了这样一段代码,其中括号语句在关键字’try’之后。 这有效吗? 为什么不能eclipse识别它并报告语法错误?

为什么我的NullPointerException没有被我的catch块捕获?

我有一个线程,我在一个大的,无所不包的catch块中捕获所有错误。 我这样做,以便我可以在我的应用程序中报告任何错误,而不仅仅是预期的错误。 我的Runnable看起来像这样: public final void run() { try { System.out.println(“Do things”); /* [1] */ doUnsafeThings(); } catch (Throwable t) { System.out.println(“Catch”); /* [2] */ recover(); } finally { System.out.println(“Finally”); /* [3] */ } } 我希望NPE能被Throwable catch块捕获。 相反,不打印[2]处的输出,也不打印[3]。 打印[1]处的输出。 我在控制台上得到的是这样的: Uncaught exception java/lang/NullPointerException. 到底是怎么回事? 对于法庭记录,我正在使用J2ME,这是在Sun的WTK v2.5.2模拟器中运行的。 我很想把它归结为JVM实现的笨拙,但我不禁感到我只是错过了一些东西。 澄清是为了避免怀疑(因为示例代码明显改变了我的生产代码) run方法中try / catch / finally块之外没有任何内容。 每个块的开头都有一个System.out.println – […]

抓住Throwable进行清理是否可以?

举个这样的例子: public List readThings(List files) throws IOException { ImmutableList.Builder things = ImmutableList.builder(); try { for (File file : files) { things.add(readThing(file)) } return things.build(); } catch (Throwable t) { for (CloseableThing thing : things.build()) { thing.close(); } throw t; } } 代码审查评论的出现是因为通常有一条规则不能捕获Throwable。 执行此类仅故障清理的旧模式是: public List readThings(List files) throws IOException { ImmutableList.Builder things = ImmutableList.builder(); boolean […]

如何循环try catch语句?

你如何循环try / catch语句? 我正在制作一个使用扫描仪读取文件的程序,它正在从键盘上读取它。 所以我想要的是如果文件不存在,程序会说“这个文件不存在请再试一次”。 然后让用户键入不同的文件名。 我尝试了几种不同的方法尝试这样做但是,我的所有尝试最终导致程序崩溃。 这就是我所拥有的 try { System.out.println(“Please enter the name of the file: “); Scanner in = new Scanner(System.in); File file = new File(in.next()); Scanner scan = new Scanner(file); } catch (Exception e) { e.printStackTrace(); System.out.println(“File does not exist please try again. “); }

在try catch中访问变量

我一直在返回menuFont行上遇到编译错误,它说没有变量menuFont。 有人可以告诉我如何解决这个问题。 import java.awt.Font; import java.awt.FontFormatException; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class loadFiles { Font getFont(){ try { Font menuFont = Font.createFont( Font.TRUETYPE_FONT, new FileInputStream(“font.ttf”)); } catch (FileNotFoundException e) { System.out.println(“Cant find file.”); e.printStackTrace(); } catch (FontFormatException e) { System.out.println(“Wrong file type.”); e.printStackTrace(); } catch (IOException e) { System.out.println(“Unknown error.”); e.printStackTrace(); } return […]

块捕获中的丢失exception

我运行这段代码: public class User { public static void main(String args[]) { int array[] = new int[10]; int i = 1; try { System.out.println(“try: ” + i++); System.out.println(array[10]); System.out.println(“try”); } catch (Exception e) { System.out.println(“catch: ” + i++); System.out.println(array[10]); System.out.println(“catch”); } finally { System.out.println(“finally: ” + i++); Object o = null; o.hashCode(); System.out.println(“finally”); } } } […]

Java ConcurrentHashMap不是线程安全的..是吗?

之前我正在使用HashMap public Map clients = new HashMap(); 现在我已经切换到ConcurrentHashMap以避免同步块,现在我遇到了问题,我的服务器每秒都有大量加载200-400个并发客户端,预计会随着时间的推移而增长。 现在看起来像这样 public ConcurrentHashMap clients = new ConcurrentHashMap(); 我的服务器设计就像这样。 我有一个工作线程来处理大量的数据包。 每个数据包都使用packetHandler子程序(不是线程的一部分)进行检查,几乎任何客户端都可以在任何时候调用它,它几乎就像静态但不是。 我的整个服务器大多是单线程的,除了数据包处理部分。 无论如何,当有人使用命令时,如在线计算所有客户并从中获取一些信息。 客户端也可能会在计数正在进行时断开连接并从ConcurrentHashMap中删除(这会导致我的问题)。 另外我想在这里添加一些代码。 int txtGirls=0; int vidGirls=0; int txtBoys=0; int vidBoys=0; Iterator i = clients.values().iterator(); while (i.hasNext()) { UserProfile person = (UserProfile)i.next(); if(person != null) { if(person.getChatType()) { if(person.getGender().equals(“m”)) vidBoys++; else //<– crash occurs here. vidGirls++; […]