i received this error from my code and can't seem to find a solution. This is my first time handling throw exception in java. Any help is appreciated!
C:\Users\acer\Documents\MyFinal3.java:5: error: ';' expected
static void exceptionFinal() throw RuntimeException();{
1 error
import java.io.*;
import java.util.*;
public class MyFinal3
{
static void exceptionFinal() throw RuntimeException eE{
System.out.println("Inside exceptionFinal");
throw RuntimeException();
}
public static void main(String []args)
{
double myDouble[] = new double[5];
try {
exceptionFinal();
System.out.println("Access element sixth :" +
myDouble[6]);
}
catch (RuntimeException eE) {
System.out.println("Exception thrown: 1");
}
catch (Exception eE) {
System.out.println("Exception thrown: 2");
}
catch (ArrayIndexOutOfBoundsException eE) {
System.out.println("Exception thrown: 3" );
}
finally {
System.out.println("Exception end" );
}
System.out.println("Out of the block");
}
}
your code has multiple issues and surely shows the lack of basic understanding of java, but in order to compile your current code, you should rewrite it as follows. Note the differences in the usage of throw and throws. As one of the comment suggested, please review Difference between throw and throws in Java?
import java.io.*;
import java.util.*;
public class MyFinal3 {
static void exceptionFinal() throws RuntimeException {
System.out.println("Inside exceptionFinal");
throw new RuntimeException();
}
public static void main(String[] args) {
double myDouble[] = new double[5];
try {
exceptionFinal();
System.out.println("Access element sixth :" + myDouble[6]);
} catch (RuntimeException eE) {
System.out.println("Exception thrown: 1");
} catch (Exception eE) {
System.out.println("Exception thrown: 2");
}
finally {
System.out.println("Exception end");
}
System.out.println("Out of the block");
}
}