the errors arriving during the execution of a program when there is a mistake of the programmer in applying the correct mathematical logic
eg: int a= 5;
int b=0;
System.out.print(a/b);
System.out.print("correct answer");
in this eg. a runtime error will be shown and to remove it we can use try catch block
Here is a code snippet illustrating exception handling: try { int a= 3 / 0 ; } catch ( ArithmeticException e ) { System.out.println ("An ArithmeticException has occured"); } finally { // some code }
In Java, errors that arise during the execution of the program are more formally referred to as Exceptions. Exceptions can be handled using try catch blocks. Here is an example : try { int answer = 42 / 0 ; } catch ( ArithmeticException e ) { e.printStackTrace(); }
public static void main(String[] args) { int a = 5; int b = 10; int c; c = a + b; // addition c = a - b; // subtraction c = a * b; // multiplication c = a / b; // division }
The throw keyword is used from within a method to "throw" an exception to the calling method. In order to throw an exception, the method signature must indicate that it will throw an exception. For example: public void foo() throws Exception { } When you want to actually throw the exception, you can do it a few different ways: throw new Exception("Exception message"); or from within a catch block ( catch(Exception ex) ): throw ex;
import java.util.Scanner; class NoMatchException extends Exception { static void method() throws Exception{ String s = "India"; String s2 = " "; try { if (!s.equals(s2)){ throw new NoMatchException(); //System.out.println("Strings are equal"); } else { System.out.println("Strings are equal"); //throw new NoMatchException(); } } catch(NoMatchException ne){ System.out.println("Exception Found For String"); } finally { System.out.println("In Finally Block"); } } } public class BasicException { static void myMethod(int x, int y) throws Exception{ try { if(x < y ) { System.out.println("Y is greater then X"); throw new Exception(); } else System.out.println("Y is smaller"); } catch(Exception ex) { System.out.println("exception found"); //System.out.println(ex.toString()); } finally { System.out.println("FINALLY block"); } } public static void main(String args[]) throws Exception{ Scanner s = new Scanner(System.in); int d, a, x, y; System.out.println("Enter the value of x and y"); x = s.nextInt(); y = s.nextInt(); try { BasicException.myMethod(x , y); }catch(Exception e) { System.out.println("main method exception"); System.out.println(e.toString()); } try { NoMatchException.method(); } catch(NoMatchException e) { System.out.println("Exception Caught"); } try{ d=0; a=42/d; System.out.println("This will not be printed."); } catch(ArithmeticException e){ System.out.println("Division by zero."); } } }
% and /
// A method which throws an exception. // Declare an ArithmeticException to be thrown. int integerDivision(int a, int b) throws ArithmeticException { // If we try to divide by zero, throw our exception... if(b == 0) { throw new ArithmeticException("Division by 0"); } // ...otherwise, return our result. return a/b; } // A method which catches an exception. void doSomeDivision() { // Let's divide each integer [0,9] by one another... for(int a = 0; a < 10; ++a) { for(int b = 0; b < 10; ++b) { // Try to do the division... try { final int q = integerDivision(a,b); System.out.println(a + " / " + b + " = " + q); } catch(ArithmeticException ex) { // ...end up here in case of Exception System.out.println("Cannot divide " + a + " by " + b); } } } }
Here is a code snippet illustrating exception handling: try { int a= 3 / 0 ; } catch ( ArithmeticException e ) { System.out.println ("An ArithmeticException has occured"); } finally { // some code }
In Java, errors that arise during the execution of the program are more formally referred to as Exceptions. Exceptions can be handled using try catch blocks. Here is an example : try { int answer = 42 / 0 ; } catch ( ArithmeticException e ) { e.printStackTrace(); }
Depends entirely on the programming language in question. Examples: C#: DivideByZeroException Java: ArithmeticException Python: ZeroDivisionError C++: No exception, let's the CPU it's been compiled for handle it. So whatever it would do in Assembly/Machine is what it does.
The throw keyword in java causes a declared exception to be thrown. For example, if you want to mark an error in a method that add two numbers if any of then is negative you can write something like this:public int addMethod( int a, int b ){if ( a < 0 b < 0 ){throw new ArithmeticException("negative addition");}return a + b;}The previous code cause a ArithmeticException if some try to add negative numbers.
Exceptions are thrown when Java encounters an error. They are a fundamental part of the Java error handling system. In a nutshell, Java will throw an Exception when it encounters an error so that an exception handler can "handle" the error. For instance, if a calculator program is given the command "1/0", Java can throw an ArithmeticException which could be reported back to the user.Some common types are:ArithmeticException - thrown when arithmetic error has occurred, like dividing by 0IOException - thrown when an input or output error has occurred, like a file not foundArrayIndexOutOfBoundsException - thrown when a nonexistent element of an array is requested, like arr[-1]NullPointerException - thrown when some operation if performed on an object whose value is nll.
public static void main(String[] args) { int a = 5; int b = 10; int c; c = a + b; // addition c = a - b; // subtraction c = a * b; // multiplication c = a / b; // division }
The throw keyword is used from within a method to "throw" an exception to the calling method. In order to throw an exception, the method signature must indicate that it will throw an exception. For example: public void foo() throws Exception { } When you want to actually throw the exception, you can do it a few different ways: throw new Exception("Exception message"); or from within a catch block ( catch(Exception ex) ): throw ex;
import java.util.Scanner; class NoMatchException extends Exception { static void method() throws Exception{ String s = "India"; String s2 = " "; try { if (!s.equals(s2)){ throw new NoMatchException(); //System.out.println("Strings are equal"); } else { System.out.println("Strings are equal"); //throw new NoMatchException(); } } catch(NoMatchException ne){ System.out.println("Exception Found For String"); } finally { System.out.println("In Finally Block"); } } } public class BasicException { static void myMethod(int x, int y) throws Exception{ try { if(x < y ) { System.out.println("Y is greater then X"); throw new Exception(); } else System.out.println("Y is smaller"); } catch(Exception ex) { System.out.println("exception found"); //System.out.println(ex.toString()); } finally { System.out.println("FINALLY block"); } } public static void main(String args[]) throws Exception{ Scanner s = new Scanner(System.in); int d, a, x, y; System.out.println("Enter the value of x and y"); x = s.nextInt(); y = s.nextInt(); try { BasicException.myMethod(x , y); }catch(Exception e) { System.out.println("main method exception"); System.out.println(e.toString()); } try { NoMatchException.method(); } catch(NoMatchException e) { System.out.println("Exception Caught"); } try{ d=0; a=42/d; System.out.println("This will not be printed."); } catch(ArithmeticException e){ System.out.println("Division by zero."); } } }
class CatchException { public static void main(String args[]) { try { int j=45/0; }catch(Exception exp){System.out.println("Exception caught "+exp.stacktrace());} } }
K u v e m p u U n i v e r s i t yLaboratory AssignmentsSubject: Basics of .NETSubject Code: BSIT - 61 L1. Write a program in C# using command line arguments to display a welcomemessage. The message has to be supplied as input in the command line.using System;using System.Collections.Generic;using System.Text;namespace Exercise_1{class Program{static void Main(string[] args){for (int i = 0; i < args.Length; i++){Console.Write(args[i] + " ");}Console.ReadLine();}}}2. Write a program in C# to find the area of a circle, given its radius.using System;using System.Collections.Generic;using System.Text;namespace Exercise_2{class Program{static void Main(string[] args){Console.WriteLine("Enter the radius of Circle to find its area");double radius =double.Parse(Console.ReadLine());double area = Math.PI * radius * radius;Console.WriteLine("The area of circle for given radius is : " + area);Console.ReadLine();}}}3. Write a program in C# to find the area of polygon - triangle and rectangle byusing multiple constructors.using System;using System.Collections.Generic;class rkm{public rkm(double basse,double height) //constructor for triangle{double b = basse;double h = height;double tri_area = (0.5) * b * h;Console.WriteLine("Area of Triange:- " + tri_area);}public rkm(float width, floatheight) //constructor for Rectangle{float w = width;float h = height;float rec_area = w * h;Console.WriteLine("Area of Rectangle:- "+ rec_area);}public static void Main(){Console.WriteLine("============For Triangle==============");Console.WriteLine("Enter the base of triangle:-");double tri_base =Convert.ToDouble(Console.ReadLine());Console.WriteLine("Enter the height of triangle:-");double tri_height =Convert.ToDouble(Console.ReadLine());Console.WriteLine("============For Rectangle==============");Console.WriteLine("Enter the width of rectangle:-");float rec_width = (float)(Convert.ToDouble(Console.ReadLine()));Console.WriteLine("Enter the height of rectangle:-");float rec_height = (float)(Convert.ToDouble(Console.ReadLine()));Console.WriteLine("================Result=================");rkm th1 = new rkm(tri_base, tri_height);rkm th2 = new rkm(rec_width, rec_height);Console.ReadLine();}}4. Write a program in C# to add and multiply two numbers and display the results.Demonstrate the use of ref and out parameters in this program.using System;using System.Collections.Generic;using System.Text;namespace Exercise_4{class Program{public static int ADD(ref int x,ref int y){int res = x + y;return res;}public static int Multiply(int x,int y, out int res1){res1 = x * y;return res1;}static void Main(string[] args){Console.WriteLine("Enter First Number");int n1 =Convert.ToInt32(Console.ReadLine());Console.WriteLine("Enter Second Number");int n2 =Convert.ToInt32(Console.ReadLine());int Res = Program.ADD(refn1, ref n2);Console.WriteLine("Sum of Two Number is : "+ Res);Program.Multiply(n1, n2, outRes);Console.WriteLine("Multiply of Two Number is : " + Res);Console.ReadLine();}}}5. Using foreach looping statement, write a program in C# to display all theelements of string array.using System;using System.Collections.Generic;using System.Text;namespace Exercise_5{class Program{static void Main(string[] args){string[] str = { "I","Love", "My", "India"};foreach (string s instr){Console.WriteLine(s);}Console.ReadLine();}}}6. Write a program in C# to create a string array to store 10 employees name.Display the employees name to the console by passing array as a parameter tothe display function.using System;using System.Collections.Generic;using System.Text;namespace Exercise_6{class Program{public static void Display(Arrayarr){foreach (string a inarr){Console.WriteLine(a);}}static void Main(string[] args){string[] arr = new string[10];for (int i = 0; i < arr.Length; i++){Console.WriteLine("Enter " + (i + 1) + " Emplyee Name");string str = Console.ReadLine();arr.SetValue(str, i);}Console.WriteLine("The Names you have Entered is given below ");Console.WriteLine();Display(arr);Console.ReadLine();}}}7. Write a program in C# to demonstrate the usage of checked and uncheckedstatements.using System;class rkm{public static void Main(){int i = Int32.MaxValue;Console.WriteLine("Simple: " + (i + 1));unchecked{Console.WriteLine("Unchecked: " + (i + 1));}checked{try{Console.WriteLine(i + 1);}catch (Exception ee){Console.WriteLine("Error in Checked Block");Console.ReadLine();}}}}8. Write a class called rectangle which consists of members side_1, side_2 anddisplayArea(). Write another class square which inherits class rectangle. Takeside of square as input and display the area of square on console.using System;using System.Collections.Generic;using System.Text;public class RKM{public double side_1;public double side_2;public void displayArea(){double area = side_1 * side_2;Console.WriteLine("Area is:- " + area);}}class Square : RKM{public static void Main(){Square sq = new Square();Console.WriteLine("Enter the side of Square:-");sq.side_1 =Convert.ToDouble(Console.ReadLine());sq.side_2 = sq.side_1;sq.displayArea();Console.ReadLine();}}9. Write a program to sort 20 decimal numbers in decreasing order and print theresult.using System;using System.Collections.Generic;using System.Text;class RKM{public static void Main(string[] args){decimal[] arr = { 12.3m, 45.2m, 5.4m, 23.44m, 43.21m, 31.34m, 4.3m, 21.3m,34.2m, 32.1m, 44.2m, 6.4m, 64.3m, 3.4m, 5.32m, 32.345m, 4.34m, 23.4m,45.234m, 5.31m };for (int i = 0; i < arr.Length; i++){Console.WriteLine(arr[i]);}Console.WriteLine("\n--Number in decreasing order--\n");int count = 0;while (count < arr.Length - 1){if (arr[count] < arr[count + 1]){decimal temp = arr[count];arr[count] = arr[count + 1];arr[count + 1] = temp;count = 0;continue;}count++;}foreach (double i inarr){Console.WriteLine(i);Console.ReadLine();}}}10. Write a program for matrix multiplication in C#.using System;using System.Collections.Generic;using System.Text;namespace Exercise_10{class Program{static void Main(string[] args){int[,] x ={ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };int[,] y ={ { 11, 12, 13 }, { 14, 15, 16 }, { 17, 18, 19 } };Console.WriteLine("----First Matrix--------");for (int i = 0; i < 3; i++){for (int j = 0; j < 3; j++)Console.Write(x[i, j] + " ");Console.WriteLine();}Console.WriteLine("----Second Matrix--------");for (int i = 0; i < 3; i++){for (int j = 0; j < 3; j++)Console.Write(y[i, j] + " ");Console.WriteLine();}Console.WriteLine("Multilpilcation of Two Matrix (Matrix1 * Matrix2) ");for (int i = 0; i < 3; i++){for (int j = 0; j < 3; j++)Console.Write(x[i, j] * y[i, j] + " ");Console.WriteLine();}Console.ReadLine();}}}11. Write a program in C# to sort the students list based on their names. Thestudents list is stored in a string array.using System;using System.Collections.Generic;using System.Text;namespace Exercise_11{class Program{public static void Display(Arrayarr){foreach (string s inarr){Console.WriteLine(s);}}static void Main(string[] args){Console.WriteLine("Enter the No of Student");int n =Convert.ToInt16(Console.ReadLine());string[] arr = new string[n];for (int i = 0; i < arr.Length; i++){Console.WriteLine("Enter " + (i + 1) + " Student Name");string str = Console.ReadLine();arr.SetValue(str, i);}Console.WriteLine("\nThe Names you have Entered i Sort order is givenbelow ");Console.WriteLine();Array.Sort(arr);Display(arr);Console.ReadLine();}}}12. Write a program in C# to demonstrate operator overloading.using System;class rkm{int a;public rkm(){a = 10;}public static rkm operator-(rkm obj){obj.a = -obj.a;return obj;}public void display(){Console.WriteLine("Number is {0}", a);}public static void Main(){rkm aa = new rkm();aa.display();aa = -aa;aa.display();Console.ReadLine();}}13. Write a program in C# to demonstrate boxing and unboxing opertation.using System;using System.Collections.Generic;using System.Text;namespace Exercise_13{class Program{static void Main(string[] args){int i = 10;Console.WriteLine("Value of i : " + i);object obj = (object)(i+1); // boxing Convert int to objectConsole.WriteLine("Value of obj :"+ obj);i = (int)obj;Console.WriteLine("Value of i after unbox "+i);Console.ReadLine();}}}14. Write a program in C# to throw and catch any arithmetic exception.using System;using System.Collections.Generic;using System.Text;namespace Exercise_14{class Program{static void Main(string[] args){try{int n1 =10;int n = 0;int res;res = n1 / n;Console.WriteLine(res);}catch (ArithmeticException ex){Console.WriteLine(ex.Message.ToString());}Console.ReadLine();}}}15. Write a program to demonstrate the usage of threads in C#.using System;using System.Threading;class rkm{public void get1(){for (int i = 0; i