Program
1
public void init ()
{
String squareSizeParam = getParameter ("squareSize");
parseSquareSize (squareSizeParam);
String colorParam = getParameter ("color");
Color fg = parseColor (colorParam);
setBackground (Color.black);
setForeground (fg);
}
private void parseSquareSize (String param)
{
if (param == null) return;
try
{
squareSize = Integer.parseInt (param);
}
catch (Exception e)
{
}
}
Program
2
class Animal
{
public void move()
{
System.out.println("Animals can move");
}
}
class Dog extends Animal
{
public void move()
{
System.out.println("Dogs can walk and run");
}
}
public class TestDog
{
public static void main(String args[])
{
Animal a = new Animal();
Animal b = new Dog();
a.move();
b.move();
}
}
Program 3
class Animal
{
public void move()
{
System.out.println("Animals can move");
}
}
class Dog extends Animal
{
public void move()
{
System.out.println("Dogs can walk and run");
}
public void bark()
{
System.out.println("Dogs can bark");
}
}
public class TestDog
{
public static void main(String args[])
{
Animal a = new Animal();
Animal b = new Dog();
a.move();
b.move();
b.bark();
}
}
Program
4
package com.journaldev.exceptions;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ExceptionHandling
{
public static void main(String[]
args) throws FileNotFoundException, IOException
{
try
{
testException(-5);
testException(-10);
}
catch(FileNotFoundException
e)
{
e.printStackTrace();
}catch(IOException
e)
{
e.printStackTrace();
}
finally
{
System.out.println("Releasing
resources");
}
testException(15);
}
public static void testException(int
i) throws FileNotFoundException, IOException
{
if(i
< 0)
{
FileNotFoundException myException = new FileNotFoundException("Negative
Integer "+i);
throw
myException;
}
else
if(i > 10)
{
throw
new IOException("Only supported for index 0 to 10");
}
}
}
Program
5
package com.journaldev.exceptions;
public class MyException extends Exception
{
private static final long
serialVersionUID = 4664456874499611218L;
private String
errorCode="Unknown_Exception";
public MyException(String
message, String errorCode)
{
super(message);
this.errorCode=errorCode;
}
public
String getErrorCode()
{
return
this.errorCode;
}
}
Program
6
package com.journaldev.exceptions;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class CustomExceptionExample
{
public static void main(String[]
args) throws MyException
{
try
{
processFile("file.txt");
}
catch (MyException e)
{
processErrorCodes(e);
}
}
private static void
processErrorCodes(MyException e) throws MyException
{
switch(e.getErrorCode())
{
case
"Bad_file_type":
System.out.println("Bad
File Type, notify user");
throw
e;
case
"File_not_found_exception":
System.out.println("File
Not Found, notify user");
throw
e;
case
"File_close_exception":
System.out.println("File
Close failed, just log it.");
break;
default:
System.out.println("Unknown
exception occured, lets log it for further debugging."+e.getMessage());
e.printStackTrace();
}
}
private static void
processFile(String file) throws MyException
{
InputStream
fis = null;
try
{
fis
= new FileInputStream(file);
}
catch
(FileNotFoundException e)
{
throw
new MyException(e.getMessage(),"File_not_found_exception");
}
finally
{
try
{
if(fis
!=null)fis.close();
}
catch (IOException e)
{
throw
new MyException(e.getMessage(),"File_close_exception");
}
}
}
}
No comments:
Post a Comment