We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Java Exception Handling (Try-catch)
Java Exception Handling (Try-catch)
+ 0 comments import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ try{ Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int y = sc.nextInt(); int result = x/y; System.out.println(result); } catch(ArithmeticException ae){ System.out.println("java.lang.ArithmeticException: / by zero"); } catch(InputMismatchException ime){ System.out.println("java.util.InputMismatchException"); } } }
+ 0 comments import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); try { int x = scanner.nextInt(); int y = scanner.nextInt(); System.out.println(x/y); } catch (InputMismatchException e) { System.out.println(e.getClass().getName()); } catch (Exception e) { System.out.println(e); } finally { scanner.close(); } } }
+ 0 comments import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner input = new Scanner(System.in); int x; int y; try{ x = input.nextInt(); y = input.nextInt(); float resp = x/y; if(resp%10==0){ System.out.println(Math.round(resp)); }else{ System.out.println(y); } }catch(InputMismatchException e){ System.out.println(e.getClass().toString().substring(6)); }catch(ArithmeticException a){ System.out.println(a); } } }
+ 0 comments Here are the solution of Java Exception Handling (Try-catch) Hacker Rank Solution
+ 0 comments import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String x1 = scan.nextLine(); String y1 = scan.nextLine(); try{ int x = Integer.parseInt(x1); int y = Integer.parseInt(y1); if(x==0||y==0){ System.out.println("java.lang.ArithmeticException: / by zero"); } else{ System.out.println(x/y); } } catch(Exception ex){ System.out.println("java.util.InputMismatchException"); } } }
Load more conversations
Sort 266 Discussions, By:
Please Login in order to post a comment