Using a Exception's instance in catch clause - java

public class ExceptionObject {
public static void main(String[] args)
{
Exception exceptionObj = new Exception();
int a=1, b=0;
try
{
int c=a/b;
}
catch(exceptionObj)
{
exceptionObj.printStackTrace();
}
}
}
Why cant I use the "exceptionObj" which is an instance of the class Exception in catch clause.
Please advise, would be helpful.

You can try this:
int a = 1, b = 0;
try {
int c = a / b;
} catch (Exception exceptionObj) {
exceptionObj.printStackTrace();
}

Related

Mockito to test the catch block of private method

I need to write a test to verify that when an IOException is thrown by the private method_C, Method_B returns True.
But
public final class A{
public static Boolean Method_B(){
try{
//call a private method C which throws IOException
Method_C
}
catch(final IOException e) {
return Boolean.True
}
}
private static Method_C() throws IOException {
return something;
}
What I tried:
#Test
public void testSomeExceptionOccured() throws IOException {
A Amock = mock(A.class);
doThrow(IOException.class).when(Amock.Method_C(any(),any(),any(),any()));
Boolean x = A.Method_B(some_inputs);
Assert.assertEquals(Boolean.TRUE, x);
}
I am getting compilation errors :
1.Cannot mock a final class
2. Method_C has private access in A
Any suggestions on how this can be rectified?
you are required to use finally in try catch
import java.io.*;
public class Test {
public static Boolean Method_B() {
try {
System.out.println("Main working going..");
File file = new File("./nofile.txt");
FileInputStream fis = new FileInputStream(file);
} catch (IOException e) {
// Exceptiona handling
System.out.println("No file found ");
} catch (Exception e) {
// Exceptiona handling
System.out.println(e);
} finally {
return true;
}
}
public static void main(String args[]) {
if (Test.Method_B()) {
System.out.println("Show true ans");
} else {
System.out.println("Sorry error occure");
}
}
}

Exception on static variables

class A {
static int a = 1 / 0;
}
In this code when we load the class, it will throw an exception because of arithmetic exception. How can I catch that exception?
use this code.
static int a=0;
try{
a = 1/0;
}catch(Exception e){
e.printStackTrace();
}
using static block
public class A {
private static int a;
static {
try {
a = 1 / 0;
} catch (Exception e) {
System.out.print("error");
}
}
}
Make it in static block.
static {
try {
Integer a = 1 / 0;
} catch (Exception e) {
}
}

How does java continue after an exception?

I've exams on Monday and was therefore doing some preparations. Now I made an exercise to see how exceptions are handled in java.
I've got the following code to analyse:
public class ExceptionsExercise {
private int x;
private class E1 extends Exception {
E1() {
super("exception E1");
}
}
private class E2 extends Exception {
E2() {
super("exception E2");
}
}
private class E3 extends Exception {
E3() {
super("exception E3");
}
}
public void run() throws E1, E3 {
try {
doA();
System.out.print("3 ");
} catch (E2 e) {
System.out.print("4 ");
} finally {
System.out.print("5 ");
}
System.out.print("6 ");
}
public void doA() throws E1, E2, E3 {
if (x == 1) {
throw new E1();
} else if (x == 2) {
throw new E2();
} else {
doB();
System.out.print("7 ");
}
}
public void doB() throws E3 {
if (x == 3) {
System.out.print("8 ");
throw new E3();
} else {
System.out.print("9 ");
}
}
public static void main(String[] args) {
Main thisInstance = new Main();
for (int i = 0; i < 4; i++) {
thisInstance.x = i;
System.out.println("");
System.out.print("x = " + i + " ");
try {
thisInstance.run();
} catch (E1 e) {
System.out.println("0");
} catch (E3 e) {
}
System.out.println("2 ");
}
}
}
The question now is what the output is. But with that I had some issues. For example, when an execption is catched in a method, does the method continue normal? Like in the method run() after catching exception e2.
The opposite situation is when a method doesn't catch an exception. Is then just the finally-block executed and then the method breaks. So that in in run() the System.out.print("6 "); is not executed?
Thank you very much
So first of all you can run your code to see the results.
when an execption is catched in a method, does the method continue normal?
Yes. I mean it will be continued farther. For example if doA() throws exception then run() method doesn't print '3'. (but it will prints '5'always because of finally
Is then just the finally-block executed and then the method breaks. So that in in run() the System.out.print("6 "); is not executed?
Finally block executes always after try block. Was exception or not.

unreported exception NegativeNumber; must be caught or declared to be thrown

I have two files:
NewtonRaphson
public class NewtonRaphson {
public double squareRoot ( double input ) throws NegativeNumber{
if (input < 0.0 ) { throw new NegativeNumber("not allowed to input a negative number");}
else if (input == 0.0) { return 0.0; }
else{
double current = 10.0;
for (int i = 0; i < 10; ++i) {
current = current - (current*current - input)/(2*current);
}
return current;
}
}
public static void main(String[] args){
NewtonRaphson nr = new NewtonRaphson();
System.out.println(nr.squareRoot(2.0));
}
}
and NegativeNumber
public class NegativeNumber extends Exception {
public NegativeNumber(String msg){
super(msg);
}
}
When I compile the first one I get:
NewtonRaphson.java:17: unreported exception NegativeNumber; must be caught or declared to be thrown
System.out.println(nr.squareRoot(2.0));
^
1 error
Could someone help me why?
You're throwing NegativeNumber, but never catching it. What kind of baseball is this?
Add a try-catch to your main method.
public static void main(String[] args){
NewtonRaphson nr = new NewtonRaphson();
try {
System.out.println(nr.squareRoot(2.0));
} catch (NegativeNumber e) {
System.out.println("Be more positive!");
}
}
Or if you never want to catch it (this is not advised):
public static void main(String[] args) throws NegativeNumber {
NewtonRaphson nr = new NewtonRaphson();
System.out.println(nr.squareRoot(2.0));
}
public static void main(String[] args){
try{
NewtonRaphson nr = new NewtonRaphson();
System.out.println(nr.squareRoot(2.0));
}
catch(NegativeNumber e)
{
System.out.println("Exception happend!");
}
}

Continue the execution after exception

I have these two Class :
public class TryException {
int a=0;
TryException(int c) {
a = c;
}
public boolean operation() //just example
{
if(a!=10)
{
System.out.println(a);
return true;
}else{
throw new RuntimeException("display something");
}
}
}
and the main :
public class Test {
static public void main(String args[])
{
int val =20;
TryException ex = new TryException(val);
try{
while(ex.operation()){
ex.a = --val;
}
}catch(RuntimeException e)
{
System.out.println("try exception");
}
}
}
when i run this program, the execution is stoped just when it detects the exception. How to continue the execution of the same while after exception ?
Move the try-catch inside the loop.
boolean run = true;
while(run){
ex.a = --val;
try{
run = ex.operation();
}catch(RuntimeException e){
System.out.println("try exception");
}
}
You need to decide when to set run to false...
It may help...
public class Test {
static public void main(String args[])
{
int val =20;
TryException ex = new TryException(val);
boolean status = true;
while(status){
try{
status = ex.operation();
} catch(RuntimeException e) {
status = true; //Or whatever...
}
ex.a = --val;
}
}
}

Categories