0% found this document useful (0 votes)
19 views3 pages

Java Eh 2

Uploaded by

varmakorni.14322
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)
19 views3 pages

Java Eh 2

Uploaded by

varmakorni.14322
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
You are on page 1/ 3

throw:

------
public class Program {
public static void main(String[] args) {
System.out.println("inside main");
try{
meth();
}catch(NullPointerException ex){
System.out.println("Exception Recaught: "+ex);
}
}
static void meth(){
System.out.println("inside meth");
//throw new NullPointerException("Testing...");
try{
throw new NullPointerException("Testing...");
}catch(NullPointerException ex){
System.out.println("Exception Caught: "+ex);
throw ex;
}
}
}
____________________________________________________________________

throws:
------

import java.io.IOException;

public class Program {


public static void main(String[] args) {
System.out.println("inside main");
try{
meth();
}catch(IOException ex){
System.out.println("Exception Caught: "+ex);
}
}
static void meth() throws IOException{
System.out.println("inside meth");
throw new IOException();
}
}

__________________________________________________________________
finally:
--------
public class Program {
public static void main(String[] args) {
methA();
methB();
try{
methC();
}catch(Exception ex){
System.out.println("Main implementation of methC's catch");
}
methD();
}
public static void methA(){
try{
System.out.println("inside methA's try");
}catch(Exception ex){
System.out.println("inside methA's catch");
}finally{
System.out.println("inside methA's finally");
}
}
public static void methB(){
try{
System.out.println("inside methB's try");
throw new Exception();
}catch(Exception ex){
System.out.println("inside methB's catch");
}finally{
System.out.println("inside methB's finally");
}
}
public static void methC()throws Exception{
try{
System.out.println("inside methC's try");
throw new Exception();
}finally{
System.out.println("inside methC's finally");
}
}
public static void methD(){
try{
System.out.println("inside methD's try");
return;
}finally{
System.out.println("inside methD's finally");
}
}
}
_____________________________________________________________________
User-defined Exception:
-----------------------
public class InvalidMarksException extends Exception{

public InvalidMarksException() {
super("Invalid Marks Entered");
}
public InvalidMarksException(String message) {
super(message);
}
}

public class Program {


public static void main(String[] args) {
try{
setMarks(-125);
}catch(InvalidMarksException ex){
System.out.println("Exception Caught: "+ex);
}
try{
setMarks(98);
}catch(InvalidMarksException ex){
System.out.println("Exception Caught: "+ex.getMessage());
}
}
public static void setMarks(int m)throws InvalidMarksException{
if((m>=0)&&(m<=100)){
System.out.println("you got "+m+" marks");
}else{
throw new InvalidMarksException("Invlaid Marks Entered: "+m);
}
}
}

You might also like