Both throw and throws are concepts of exception handling in Java. The throws keyword is used to declare which exceptions can be thrown from a method, while the throw keyword is used to explicitly throw an exception within a method or block of code.
catch statement with having throw Error(). This code will do that the 'msg' error is thrown in the try-block which will be get executed by catch statements. new Error object: It captures several properties of the place where the error happened. It exposes an error event with two parameters name & message.
new Exception() means you are creating a new instance of Exception type. Which means you are just instantiating an object similar to others like new String("abc") . You would do this when you are about to throw an exception of type Exception in next few lines of code execution.
Definition. While throw is a keyword that singles the occurrence of an exception during the program execution, throw ex is a keyword in C# that allows the programmers to indicate the occurrence of an exception which resets the stack trace. Thus, this is the main difference between throw and throw ex in C#.
throw;
rethrows the original exception and preserves its original stack trace.
throw ex;
throws the original exception but resets the stack trace, destroying all stack trace information until your catch
block.
throw ex;
throw new Exception(ex.Message);
is even worse. It creates a brand new Exception
instance, losing the original stack trace of the exception, as well as its type. (eg, IOException
).
In addition, some exceptions hold additional information (eg, ArgumentException.ParamName
).throw new Exception(ex.Message);
will destroy this information too.
In certain cases, you may want to wrap all exceptions in a custom exception object, so that you can provide additional information about what the code was doing when the exception was thrown.
To do this, define a new class that inherits Exception
, add all four exception constructors, and optionally an additional constructor that takes an InnerException
as well as additional information, and throw your new exception class, passing ex
as the InnerException
parameter. By passing the original InnerException
, you preserve all of the original exception's properties, including the stack trace.
The first preserves the original stacktrace:
try { ... }
catch
{
// Do something.
throw;
}
The second allows you to change the type of the exception and/or the message and other data:
try { ... } catch (Exception e)
{
throw new BarException("Something broke!");
}
There's also a third way where you pass an inner exception:
try { ... }
catch (FooException e) {
throw new BarException("foo", e);
}
I'd recommend using:
One other point that I didn't see anyone make:
If you don't do anything in your catch {} block, having a try...catch is pointless. I see this all the time:
try
{
//Code here
}
catch
{
throw;
}
Or worse:
try
{
//Code here
}
catch(Exception ex)
{
throw ex;
}
Worst yet:
try
{
//Code here
}
catch(Exception ex)
{
throw new System.Exception(ex.Message);
}
Throwing a new Exception blows away the current stack trace.
throw;
will retain the original stack trace and is almost always more useful. The exception to that rule is when you want to wrap the Exception in a custom Exception of your own. You should then do:
catch(Exception e)
{
throw new CustomException(customMessage, e);
}
throw
rethrows the caught exception, retaining the stack trace, while throw new Exception
loses some of the details of the caught exception.
You would normally use throw
by itself to log an exception without fully handling it at that point.
BlackWasp has a good article sufficiently titled Throwing Exceptions in C#.
throw
is for rethrowing a caught exception. This can be useful if you want to do something with the exception before passing it up the call chain.
Using throw
without any arguments preserves the call stack for debugging purposes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With