0% found this document useful (0 votes)
3 views4 pages

Java Day12

The document discusses different types of errors in programming: compile-time errors, runtime errors, and logical errors, providing examples for each. It also covers debugging techniques and exception handling in Java, including the use of try, catch, finally, throw, and throws statements. The document emphasizes the importance of identifying and fixing errors to ensure code runs correctly.

Uploaded by

myself192008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Java Day12

The document discusses different types of errors in programming: compile-time errors, runtime errors, and logical errors, providing examples for each. It also covers debugging techniques and exception handling in Java, including the use of try, catch, finally, throw, and throws statements. The document emphasizes the importance of identifying and fixing errors to ensure code runs correctly.

Uploaded by

myself192008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

/* Errors

Compile-Time Error
Runtime Error
Logical Error */

//Compile-Time Error --- Detected by the compiler. Prevents code from


running.
/* public class Day12 {
public static void main(String[] args) {
int a = 6
[Link](a);
}
} */

/* public class Day12 {


public static void main(String[] args) {
[Link](x);
}
} */

/* public class Day12 {


public static void main(String[] args) {
int x = "IARE";
[Link](x);
}
} */

// Runtime Error --- Occurs while the program is running. Often causes
crashes.
/* public class Day12 {
public static void main(String[] args) {
int a = 13;
int b = 0;
int result = a / b;
[Link](result);
}
} */

/* public class Day12 {


public static void main(String[] args) {
int[] a = {5, 6, 4};
[Link](a[7]);
}
}*/

// Logical Error --- Code runs but gives incorrect results. Hardest to find.

/* public class Day12 {


public static void main(String[] args) {
int a = 10;
int b = 2;
int add = a - b;
[Link]("a + b = " + add);
}
} */

/*import [Link];
public class Day12 {
public static void main(String[] args) {
Scanner myObj = new Scanner([Link]);
[Link]("Enter the number a");
int a = [Link]();
[Link]("Enter the number b");
int b = [Link]();
int add = a - b;
[Link]("a + b = " + add);
}
} */

// Debugging is the process of identifying and fixing errors or bugs in your code.

/* public class Day12 {


public static void main(String[] args) {
int a = 13;
int b = 0;
[Link]("Before division");
int result = a / b;
[Link](result);
}
} */

/* public class Day12 {


public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4};
for (int i = 0; i <= [Link]; i++) {
[Link](numbers[i]);
}
}
} */

// try, catch, finally, throw, throws


// The try statement allows you to define a block of code to be tested for errors
while it is being executed.
// The catch statement allows you to define a block of code to be executed, if an
error occurs in the try block.

/* public class Day12 {


public static void main(String[] args) {
try {
int[] a = {5, 6, 4};
[Link](a[7]);
} catch (Exception e) {
[Link]("Something went wrong.");
}
}
} */

/* public class Day12 {


public static void main(String[] args) {
try {
int result = 10 / 0; // Risky code
} catch (ArithmeticException e) {
[Link]("Exception caught: " + e);
}
[Link]("Program continues...");
}
} */
/* public class Day12 {
public static void main(String[] args) {
String x = null;
String z = [Link]();
[Link](z);
}
} */

/* public class Day12 {


public static void main(String[] args) {
String x = null;
try {
String z = [Link](); // Will cause NullPointerException
} catch (NullPointerException e) {
[Link]("Outer catch: " + e);
}
[Link]("Program continues...");
}
} */

/* public class Day12 {


public static void main(String[] args) {
try {
try {
int num = 10 / 0;
} catch (ArithmeticException e) {
[Link]("Inner catch: " + e);
}
String str = null;
[Link]([Link]());
} catch (NullPointerException e) {
[Link]("Outer catch: " + e);
}
[Link]("Program continues...");
}
} */

// The finally statement lets you execute code, after try...catch, regardless of
the result:

/* public class Day12 {


public static void main(String[] args) {
try {
int[] myNumbers = {1, 2, 3};
[Link](myNumbers[10]);
} catch (Exception e) {
[Link]("Something went wrong.");
} finally {
[Link]("The 'try catch' is finished.");
}
}
} */

// The "throw" keyword in Java is used to explicitly throw an exception.


/* public class Day12 {
//defining a method
public static void checkNum(int num) {
if (num < 1) {
throw new ArithmeticException("\nNumber is negative, cannot calculate
square");
}
else {
[Link]("Square of " + num + " is " + (num*num));
}
}
//main method
public static void main(String[] args) {
TestThrow obj = new TestThrow();
[Link](-3);
[Link]("Rest of the code..");
}
} */

// the "throws" clause is used in method signatures to indicate that the method may
throw certain types of exceptions during its execution.

/* public class Day12 {


public static int divideNum(int m, int n) throws ArithmeticException {
int div = m / n;
return div;
}
public static void main(String[] args) {
Day12 obj = new Day12();
try {
[Link]([Link](45, 0));
}
catch (ArithmeticException e){
[Link]("\nNumber cannot be divided by 0");
}

[Link]("Rest of the code..");


}
} */

/* public class Day12 {


static void method() throws ArithmeticException
{
[Link]("Inside the method()");
throw new ArithmeticException("throwing ArithmeticException");
}
public static void main(String args[])
{
try
{
method();
}
catch(ArithmeticException e)
{
[Link]("caught in main() method");
}
}
} */

You might also like