Professional Documents
Culture Documents
Class Report
Class Report
On Monday 4th March,2019 at 7:30am, we had a java class at NNB3 discussing about
the idea of exceptions in java.
According to www.journaldev.com "Exception is an error event that can happen during
the execution of a program and disrupts its normal flow".Exception can arise from
different kind of situations such as wrong data entered by user, hardware failure,
network connection failure, Database server down etc. In this section, we will
learn how exceptions are handled in java.
So actually the purpose of the lecture was to educate us on the expected errors we
could have when writing a program and the ways we could handle such erros
called"Exception handling". Java Exception handling is a framework that is used to
handle runtime errors only.
Some keywords used in exception handling
1. throw:Sometime we might want to generate exception explicitly in our code, for
example in a user authentication program we should throw exception to client if the
password is null. throw keyword is used to throw exception to the runtime to handle
it.
example
public class ThrowExample {
static void checkEligibilty(int stuage, int stuweight){
if(stuage<12 && stuweight<40) {
throw new ArithmeticException("Student is not eligible for registration");
}
else {
System.out.println("Student Entry is Valid!!");
}
}
num1 = 0;
num2 = 62 / num1;
System.out.println(num2);
System.out.println("Hey I'm at the end of try block");
}
catch (ArithmeticException e) {
System.out.println("Exception occurred");
}
System.out.println("I'm out of try-catch block in Java.");
}
}www.beginnersbook.com
3.Finally:finally block is optional and can be used only with try-catch block.
Since exception halts the process of execution, we might have some resources open
that will not get closed, so we can use finally block. finally block gets executed
always, whether exception occurred or not.
example
class finally
{
public static void main(String args[]) {
try{
int num=121/0;
System.out.println(num);
}
catch(ArithmeticException e){
System.out.println("Number should not be divided by zero");
}
finally{
System.out.println("This is finally block");
}
System.out.println("Out of try-catch-finally");
}
}beginnersbook.com
you can handle a specific exception or a general exception by using the above
codes.
Array.index out of bound exception is a class in java
Array.index out of bounds exception
An array-index out of bounds exception is a Java exception thrown due to the fact
that the program is trying to access an
element at a position that is outside an array limit, hence the words "Out of
bounds". In other words, the program is trying to
access an element at an index that is outside the array bounds. To understand array
bounds, let us consider the following diagram:
An example to demonstrate the Array.inex out of bounds exception
public class OutOfBounds {
System.out.println(".."+array[i]);
}
}
}Source reference www.voidexception.weebly.com
example 2
public class OutOfBounds {
}
}
We also looked at how to write your code to prevent exceptions, looking at how to
handle a specific exception or a genereal exception
}
}Class example
How to write your code to prevent exceptions
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.*;
class GFG
{
public static void main (String[] args)
{
try
{
if (ptr.equals("gfg"))
System.out.print("Same");
else
System.out.print("Not Same");
}
catch(NullPointerException e)
{
System.out.print("NullPointerException Caught");
}
}
} www.geeksforgeeks.org
We were asked to google search for the types of exceptions and use them to write a
program
Write five programs that demonstrate five different exceptions(Assignment)
Assignment
create a file called data.txt
The quick brown fox jumps over the lazy dog.
Write a java program that reads the content of data.txt and displays the content in
the console
A class called buffered reader and file reader