├── JavaExceptionHandling └── JavaExceptionHandling1 /JavaExceptionHandling: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | public class Solution { 4 | public static void main(String[] args) { 5 | Scanner sc=new Scanner(System.in); 6 | try{ 7 | int x=sc.nextInt(); 8 | int y=sc.nextInt(); 9 | System.out.println(x/y); 10 | } 11 | catch(InputMismatchException c){ 12 | System.out.println(c.getClass().getName()); 13 | } 14 | catch(Exception e){ 15 | System.out.println(e); 16 | } 17 | sc.close(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /JavaExceptionHandling1: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | class MyCalculator { 3 | public long power(int n,int p)throws Exception{ 4 | if(n==0&&p==0){ 5 | throw new Exception("n and p should not be zero."); 6 | } 7 | else if(n<0||p<0){ 8 | throw new Exception("n or p should not be negative."); 9 | } 10 | else{ 11 | return (long)(Math.pow(n,p)); 12 | } 13 | } 14 | 15 | } 16 | public class Solution { 17 | public static final MyCalculator my_calculator = new MyCalculator(); 18 | public static final Scanner in = new Scanner(System.in); 19 | 20 | public static void main(String[] args) { 21 | while (in .hasNextInt()) { 22 | int n = in .nextInt(); 23 | int p = in .nextInt(); 24 | 25 | try { 26 | System.out.println(my_calculator.power(n, p)); 27 | } catch (Exception e) { 28 | System.out.println(e); 29 | } 30 | } 31 | } 32 | } 33 | --------------------------------------------------------------------------------