Showing posts with label Exception Handling. Show all posts
Showing posts with label Exception Handling. Show all posts

Exception Handling - ZeroDivisionError

 # Exception handling 


N1 = eval(input("Enter number : "))

N2 = eval(input("Enter number : "))


try:

Res = N1/N2

except ZeroDivisionError as e:

print("ZeroDivisionError : ",e)

except:

print("Unknown error")

else:

print("Division = ",round(Res,4))

finally:

print("Exception handling complete.")

"""

Sample output

>python EH3.py

Enter number : 4

Enter number : 6

Division =  0.6667

Exception handling complete.


>python EH3.py

Enter number : 4

Enter number : 'e'

Unknown error

Exception handling complete.


>python EH3.py

Enter number : 4

Enter number : 0

ZeroDivisionError :  division by zero

Exception handling complete.

"""

java - Exception handling - sum of numebrs - NumberFormatException


/*
Write a java program for exception handling:
To add two integers and raise exception when any other character except number (0 – 9) is given as input.
*/

// Java Exception Handling
// Number format exception

import java.util.*;

class numberformatexceptions
{
     public static void main(String args[])
     {
          String s1, s2;
          Scanner s = new Scanner(System.in);
         
          try
          {
              System.out.print("Enter first number : ");
              s1 = s.next();
              System.out.print("Enter second number : ");
              s2 = s.next();
             
              int x = Integer.parseInt(s1);
              int y = Integer.parseInt(s2);
             
              System.out.print("Sum = "+(x+y));
             
          }
          catch(Exception e)
          {
              System.out.println(e) ;
          }
     }
}

Java - exception handling - sum of numbers - input mismatch exception


/*
Write a java program for exception handling:
To add two integers and raise exception when any other character except number (0 – 9) is given as input.
*/

// java Exception Handling
// input mismatch exception

import java.util.*;

class inputmismatchexceptions
{
     public static void main(String args[])
     {
          int x, y;
          Scanner s = new Scanner(System.in);
         
          try
          {
              System.out.print("Enter first number : ");
              x = s.nextInt();
              System.out.print("Enter second number : ");
              y = s.nextInt();
             
              System.out.print("Sum = "+(x+y));
             
          }
          catch(Exception e)
          {
              System.out.println(e) ;
          }
     }
}

Java - exception Handling - user defined exception handling


// user defined exception

class userdefinedexception extends Exception
{
     int a;
    
     userdefinedexception(int b)
     {
          a = b;
     }
    
     public String toString()
     {
          return ("Exception Number =  "+a) ;
     }
}

class JavaException
{
     public static void main(String args[])
     {
          try
          {
              // throw is used to create a new exception and throw it.
              throw new userdefinedexception(2);
      
          }
          catch(userdefinedexception e)
          {
              System.out.println(e) ;
          }
     }
}

Java Exception Handling

/*
Write a Java program for handling the following exceptions
(i)        Arithmetic Exception          
(ii)       ArrayIndexOutOfBounds Exception
(iii)      NumberFormat Exception
(iv)      StringIndexOutOfBound Exception
*/


// Exception handling

class EH
{
     public static void main(String as[])
     {       
          int a = 5, b = 0;
          try
          {
              int d = a/b;
              System.out.println(d);             
          }
          catch(Exception e)
          {
              System.out.println(e);
          }
         
          String s = "123g";
          try
          {
              int n = Integer.parseInt(s);
              System.out.println(n);
          }
          catch(Exception e)
          {
              System.out.println(e);
          }
         
         
         
          int IA[] = new int[]{1,2,3,4,5};
          try
          {
              int x = IA[13]; 
              System.out.println(x);
          }
          catch(Exception e)
          {
              System.out.println(e);
          }
         
          String str = "HELLO JAVA";
          try
          {
              char c = str.charAt(33);
              System.out.println(c);             
          }
          catch(Exception e)
          {
              System.out.println(e);
          }
     }
}