try catch java
public class MyClass { public static void main(String[ ] args) { try { int[] myNumbers = {1, 2, 3, 4, 5, 6}; System.out.println(myNumbers[10]); } catch (Exception e) { System.out.println("Something went wrong. check again"); } } }
java try catch
try{ //Code that is checked for error } catch(Exception e){ //Code runs when error is caught }
java try and catch
import java.io.*; import java.util.Scanner; public class Test { public static void main(String args[]) throws Exception { String input; int ch1; float ch2; String ch3; Scanner one = new Scanner(System.in); input = one.nextLine(); try { ch1 = Integer.parseInt(input); System.out.println("integer"); return; } catch (NumberFormatException e) { } try { ch2 = Float.parseFloat(input); System.out.println("float"); return; } catch (NumberFormatException e) { } try { ch3 = String.valueOf(input); System.out.println("String"); } catch (NumberFormatException e) { } } }
Source: stackoverflow.com
java try and catch
int midterm; System.out.printLn("Enter midterm grade"); do { try { string s = in.nextLine(); midterm = Integer.parseInt(s); break; } catch (Exception e) { System.out.printLn("Couldn't parse input, please try again"); } } while (true);
Source: stackoverflow.com